Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 51s1 days 16 hours 1 minutes 51 seconds.

Sign in to watch this lesson
Algorithms - Bits and Number Theory · 3:13
A Gray code lists every n-bit word so that neighbours differ in exactly one bit, which is a walk along the edges of a cube that visits every corner once. We build it by reflect-and-prefix, encode with g = i ^ (i >> 1) (G(4) = 110 = 6), and decode with a running XOR from the top bit, not the tempting g ^ (g << 1). Then the surprise: in the shortest Towers of Hanoi solution, the bit that flips at step k is the disk that moves, ctz(k) + 1, and the smallest disk always cycles the same way round.