Quick question. I’ve never had experience with C.
#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;
}
This is an iterative tower of hanoi. What do things like (x&x-1) and (x|x-1)+1 mean? I figure the % is doing modulus. and %i is a way of printing out integers in C?
Thanks
(x&x-1)is equivalent to(x & (x-1)).&is the bitwise-AND operator. Similarly for the second example, where|is the bitwise-OR operator.Yes.
Yes.