Given a set I want to display all its subsets (its power set). I have found this code:
void printPowerSet(char *set, int set_size)
{
/*set_size of power set of a set with set_size
n is (2**n -1)*/
unsigned int pow_set_size = pow(2, set_size);
int counter, j;
/*Run from counter 000..0 to 111..1*/
for(counter = 0; counter < pow_set_size; counter++)
{
for(j = 0; j < set_size; j++)
{
if(counter & (1<<j))
printf("%c", set[j]);
}
printf("\n");
}
}
I can't understand why this part is used
if(counter & (1<<j))
what is its meaning?
The time complexity for this algorithm is O(n2^n)
is there any better method?