Given the bitmask, where the set bits describe where the other number can be one or zero, and the unoccupied bits must be zero in that number. What a good way to iterate through all possible values?
For example:
000 returns [000] 001 returns [000, 001] 010 returns [000, 010] 011 returns [000, 001, 010, 011] 100 returns [000, 100] 101 returns [000, 001, 100, 101] 110 returns [000, 010, 100, 110] 111 returns [000, 001, 010, 011, 100, 101, 110, 111]
The easiest way to do this is to do it like this:
void f (int m) { int i; for (i = 0; i <= m; i++) { if (i == i & m) printf("%d\n", i); } }
But this is repeated through too many numbers. It should be about 32 not 2 ** 32.
Jake
source share