From Tower of Hanoi to Counting Bits (2011)
susam.in
susam.in
max = 1 << no_of_discs;
for (x = 1; x < max; x++)
printf("move a disc from %d to %d\n", (x&x-1)%3, ((x|x-1)+1)%3);
where x&x-1 is smaller than x as much as (x|x-1)+1 is larger than x, by a 2-power corresponding to the least significant 1-bit in x.Eh?
> We will be dealing with arbitrary precision integers (bignums) in the problem, so let us also make a few assumptions:
> Addition or subtraction of an m-bit integer and an n-bit integer (m <= n) takes O(n) time.
> Counting the number of 1-bits in an n-bit integer takes O(n) time.