The "mysterious" TH algorithm explained


Here I show that the "mysterious" TH algorithm is a variation of the cyclic algorithm (see here Schoute (ref [1]); my Algorithm no. 4), which explicitly uses the move count k (denoted as x in hBits.c).
G.C. Rhoads labels the pegs 0, 1 and 2. Peg 0 is the start peg, and the final peg is either 1 (for even number of disks n) or 2 (for odd n). This has the advantage that the initial common moves are the same for all n, e.g., the first move is always from peg 0 to peg 2, etc. And the cyclic order is the same for all n: 0 > 2 > 1 > 0. This cyclic order can be written e.g. as a series of even numbers modulo 3: (2i)%3, i=0,1,2,...
Disks have the usual labels from 1 (the smallest) to n (the largest).
Odd disks are always moved according to the cyclic order, even disks in the opposite order. The cyclic algorithm consists in repeating the two following steps (an odd numbered move followed by an even numbered one):
Thus disk 1 is moved in all odd (numbered) moves (k=1,3,5,...). One starts from peg 0. In move 1 (k=1), disk 1 is moved from peg 0 to peg 2, in move 3 (k=3) from peg 2 to peg 1, in move 5 (k=5) from peg 1 to peg 0, etc., always in the cyclic order. In the above modulo 3 representation of the cyclic order this can be written as follows: disk 1 is moved in each odd move k from peg (k1)%3 to peg (k+1)%3.
If the disk moved in step 2 is also odd, it must be different from disk 1, and so be moved from peg that follows in the cyclic order the final peg of the move of disk 1 of step 1. If k is the even move number for step 2, the final peg for disk 1 in the previous odd move k1 was (k1+1)%3=k%3, and thus the step 2 move is from peg (k+2)%3 to peg (k+4)%3.
However, (k+2)%3=(k1)%3 and (k+4)%3=(k+1)%3, and thus if an odd disk is moved in move k (odd or even), the move can always be written as a move from peg (k1)%3 to peg (k+1)%3.
If the disk moved in step 2 is even, it is moved between the same two pegs, but in the direction opposite to the cyclic order. That is, from peg (k+4)%3 to peg (k+2)%3. Because in this case k>1, the starting peg of this move can also be written as (k2)%3.
Now let us denote by j the number labeling the disk that is moved in move k. By induction one has (2^{2i+1})%3 = 2 and (2^{2i})%3 = 1 for arbitrary nonnegative i. For odd j=2i1 we then have (k1)%3 = (k2^{j1})%3, and for even j=2i+2 we have (k2)%3 = (k2^{j1})%3; i>=0. Similarly for the final peg numbers. Thus for arbitrary k and j, the move can always be written as from peg (k2^{j1})%3 to peg (k+2^{j1})%3.
Next, we will use the following property of binary representation of a nonnegative integer k: if k is terminated with Z consecutive zeros preceded by a one, the binary representation of k1 ends with Z consecutive 1's preceded by a 0. Therefore, k(k1) = k+2^{Z}1 and k&(k1) = k2^{Z}, where  and & are bitwise OR and bitwise AND operations of the C language.
Finally, as mentioned before, for move k, j=Z+1. Putting all these relations together gives that in move k, disk j=Z+1 is moved from peg (k&(k1))%3 to peg (k(k1)+1)%3, which is exactly what is used in hBits.c.
(Note: For all k, one also has k^(k1) = 2^{j+1}1, where ^ is C's bitwise exclusive OR.)
The disk configuration is obtained from the following Theorem proved in [1]:
After 0 <= k <= 2^{n}1 moves,
A possible implementation of this Theorem can be find in moveConf.c, which is now also contained in the revised TH C zip archive. Its essential part is as follows:

It looks quite complicated. Would somebody be able to simplify this code?
The above Theorem can also be used in the opposite direction: given a configuration of disks, one can determine the corresponding move count k, if the configuration occurs in the minimal TH solution, or conclude that it is an illegal configuration. For further details, see Ref. [1].
References
[1]  T.R. Walsh, The Towers of Hanoi revisited: moving the rings by counting the moves, Information Processing Letters, 1982, Vol.15, 6467. Netherlands. 