Reputation: 10647
Say I want to iterate over all bit sequences (for lack of better name) in the range 0000
-1111
. When I want to scale it up to 24 bits, I won't be able to simply compute all possible permutations beforehand; I'd need to compute them on the fly.
How can I generate the permutations on-the-fly (like Python generators with yield
) using something like InputIterator
or a decorated while loop?
Upvotes: 0
Views: 736
Reputation: 56
Unless I'm misunderstanding, you can use an int (which is stored as bits anyway at it's core), just bound your loop to only iterate up to the widest int that the space would allow (in this case 24 bits). Starting at 0 and adding successive ones will implicitly step through every possible bit combo.
No need to precompute or store anything. here is a 4bit example:
for(unsigned long int cnt = 0; cnt < 16; cnt++) DoSomething(cnt);
In this case DoSomething(const unsigned long int&) could simply map the bits and perform the action you're looking for.
If your implementation supports it, use uint_32t type for an explicit and exact width.
Upvotes: 3