sping128's blog

By sping128, 11 years ago, In English

This can be easily done by:

for (i = 0; i < 1<<N; i++) {
   ...
}

It will give us: 000, 001, 010, 011, 100, 101, 110, 111 in binary if N = 3

What if I want to loop in ascending order of the number of each subset's elements? For example, when N = 3, the order can be 000, 001, 010, 100, 011, 101, 110, 111 or 000, 001, 010, 100, 011, 110, 101, 111. Anyone knows the best way to do this?

Full text and comments »

  • Vote: I like it
  • +13
  • Vote: I do not like it