Tower of Hanoi explanation

Can someone help me with this program it's about Towers of Hanoi and it's non recursive solution i found it from net and change a little bit. It's works but i want to know how it works especially this part" ((x&x-1)%3)+1 << " ---> " << (((x|x- 1)+1)%3)+1 " i understand the rest part...but this here i thinks is something with binary operations. I'm not sure. I really need an explanation.

1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>
  using namespace std;
  int main()
  {
    int n, x;
    cout << "How many disks?";
    cin >> n;
    for (x=1; x < (1 << n); x++)
        cout << "N= "<< x << "|\t"<< ((x&x-1)%3)+1 << " --->  " <<  (((x|x-1)+1)%3)+1 << endl;
    return 0;
  }
Last edited on
Topic archived. No new replies allowed.