Let's represent cominations of N-element set as N-bit numbers, where j
th bit is 1 if j
th item is included in the combintation, and 0 otherwise. This way you can represent all possible combinations as numbers in range [0, 2N).
The outer loop iterates over these numbers (1 << N
== 2N).
The inner loop iterates over items of the set and if
condition checks whether j
th item is included into the current combination. In other words, it checks if j
th bit of i
is 1.
1<<j
gives you a number where only j
th bit is 1, i & (1 << j)
resets all bits of i
other than that bit, and >
checks that result is not 0.
Note that this code (with int
s) only works for N < 31.