⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 20m 30s17 days 22 hours 20 minutes 30 seconds.
⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 20m 30s17 days 22 hours 20 minutes 30 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.