Generating all subsets from a single set
<p>I was trying to understand the code to generate all the subsets from one set. Here is the code</p>

<pre><code>#include &lt;stdio.h&gt;

/* Applies the mask to a set like {1, 2, ..., n} and prints it */
void printv(int mask[], int n) {
    int i;
    printf("{ ");
    for (i = 0; i &lt; n; ++i)
        if (mask[i])
            printf("%d ", i + 1); /*i+1 is part of the subset*/
    printf("\\b }\\n");
}

/* Generates the next mask*/
int next(int mask[], int n) {
    int i;
    for (i = 0; (i &lt; n) &amp;&amp; mask[i]; ++i)
        mask[i] = 0;

    if (i &lt; n) {
        mask[i] = 1;
        return 1;
    }
    return 0;
}

int main(int argc, char *argv[]) {
    int n = 3;

    int mask[16]; /* Guess what this is */
    int i;
    for (i = 0; i &lt; n; ++i)
        mask[i] = 0;

    /* Print the first set */
    printv(mask, n);

    /* Print all the others */
    while (next(mask, n))
        printv(mask, n);

    return 0;
}
</code></pre>

<p>I am not understand the logic behind this line <code>for (i = 0; (i &lt; n) &amp;&amp; mask[i]; ++i)</code> inside the next function. How is the next mask being generated here?</p>

<p>Code and algorithm looked here:
<a href="http://compprog.wordpress.com/2007/10/10/generating-subsets/" rel="nofollow">http://compprog.wordpress.com/2007/10/10/generating-subsets/</a></p>
