Possible Duplicate:
Creating multiple numbers with certain number of bits set
I'm attempting to write some code which will put each possible combination of numbers in an array by shifting the bits across.
For example, I wanted to find all possible combinations of 3 bits (where the max a digit can be is 6) the array should contain:
000111 001011 001101 001110 010011 010101 010110 011001 011010 011100 100011
And so on...
From what I've interpreted, when the last position bit is 1 we shift the number by 1 (x >> 1) and add a 1 at the start. However, I'm unsure how to code the rest. I'm using C to write this.
Also - as far as I can tell this is a colex sequence, however, I'm all ears if there is another sequence that will give me the same end result (array with all possible combinations of k-bits with a constraint of N).