I am looking into this iterative solution to the Towers of Hanoi problem. I specifically do not understand what is happening the logic in line 12. Any explanation is welcome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, x;
printf( "How many disks? " );
scanf( "%d", &n );
printf("\n");
for (x=1; x < (1 << n); x++)
printf( "move from tower %i to tower %i.\n",
(x&x-1)%3, ((x|x-1)+1)%3 );
return 0;
}
Ah sorry I couldn't be more help! I am curious now, so hopefully someone comes in and here explains the logic behind it. Ran the numbers on paper and it still seemed strange to me.