With a fixed number of bits, this is a bit obvious...
But for a variable number of bits, such pattern can be obtained by integer division as demonstrated here http://smallissimo.blogspot.fr/2011/07/revisiting-sieve-of-erathostenes.html
EDIT More detailed explanations below:
The idea is that :
2r01 * 2r11 -> 2r11
2r0101 * 2r11 -> 2r1111
2r010101 * 2r11 -> 2r111111
So inversely, we can apply an exact division:
2r111111 / 2r11 -> 2r010101
If we want 2r101010
rather than 2r010101
, just add 1 more bit (but the division is then inexact, I assume quotient is given by // like in Smalltalk) :
2r1111111 // 2r11 -> 2r101010
2r1111111
can be constructed easily, it is a power of 2 minus 1, 2^7-1
, which can also be obtained by a bith shift (1<<7)-1
.
In your case, your constant is ((1<<33)-1)//3
, or if you write it in C/C++ ((1ULL<<33)-1)/3
(In Smalltalk we don't care of integer length, they are of arbitrary length, but in most language we must make sure the operands fits on a native integer length).
Note that division also work for bit patterns of longer length like 2r100100100
, divide by 2r111
, and for a bit pattern of length p, divide by 2^p-1
that is (1<<p)-1