i have a problem, that i don\'t know how to solve it.
i have a binary string and i want to generate all possible binary substrings.
Example :
Nevermind--damn I forgot you had it in a string and not a int/long/whatever. You can still use the same logic though... Just count in binary but only use the positions that contain 1's.
Just thinking aloud.
Let's take the string 1001
What you want, I think, is the four numbers 1001, 1000, 0001 and 0000, is that right?
If so, what you are doing is counting the "ones" positions.
One way is to store off your original number
orig = n;
and then start iterating over that and every lower number
while(n--)
but each iteration, ignore ones that attempt to put 1's in the 0's position:
if(n & !orig)
continue;
If you expect it to be sparse--as in a lot of zeros with very few 1's, you could use a shifting mechanism. So let's say your number is 1000 1001
Notice there are three 1's, right? So just count from 000 to 111
But for each iteration, spread the bits out again:
n & 0000 0001 | n << 2 & 0000 1000 | n << 5 & 1000 0000
or the other way to think about it:
n & 001 | (n & 010) * 1000 | (n & 100) * 1000 000
This could be slower than the other solution depending on how many 1's appear though since it involves an inner loop with 1 iteration for each 1 in the original number.
Sorry about shifting between binary and decimal--I can do it all in hex if you like :)
Right now I can't come up with a pattern that directly maps bits without shifting--that would work best.