static void printPowerSet(int []set, int set_size) {
long pow_set_size = (long)Math.pow(2, set_size);
int counter, j;
System.out.println("POWER_SET of: " + Arrays.toString(set));
System.out.println("Size 2^" + set_size + " = " + pow_set_size);
/*Run from counter 000..0 to
111..1*/
for(counter = 0; counter < pow_set_size; counter++) {
System.out.print("{");
for(j = 0; j < set_size; j++) {
/* Check if jth bit in the
counter is set If set then
print jth element from set */
if((counter & (1 << j)) > 0) {
System.out.print(set[j] + "," );
}
}
System.out.print("}, ");
}
}
this was taken from GeeksForGeeks, but the part im trying to understand is this line
if ((counter & (1 << j) > 0 )
i understand the outcome and the left shifting a bit, just from reading ....ie I undertsand 0010 left shifted << becomes 1000 in binary ..which is 8 in decimal
but i'm having a hard time seeing how this if statement is being evaluated with the counter variable.
what i think i know:
the single & means both statements are evaluated regardless if true or false, but i dont see how counter is evaluated alone