Imagine a rotary shaft connected to a sensor. As the shaft turns, the sensor reads the angle and outputs it as a binary number. At the moment the angle crosses from 3 (binary 011) to 4 (binary 100), all three bits must flip simultaneously. In practice they never flip at the exact same instant — for a tiny moment the sensor can read 000, 001, 010, 100, or something else entirely. Any device downstream sees a glitch: a spurious, wrong value that can trigger the wrong action.
Frank Gray, a physicist at Bell Labs, patented the solution in 1947 and it bears his name: order the binary strings so that consecutive values differ in exactly one bit. Then the transition from 3 to 4 touches only a single bit. Only one bit changes, so there is no window in which two different bits are mid-flight and the output is undefined.
This deceptively simple idea connects to a beautiful structure in combinatorics: a Hamiltonian path on the hypercube graph whose vertices are all n-bit strings and whose edges connect strings that differ in one bit. The standard 4-bit Gray code visits all 16 vertices of a 4-dimensional hypercube, stepping along exactly one edge at a time.
The construction is recursive. The 1-bit Gray code is just [0, 1]. To get the n-bit code, write the (n–1)-bit code forward, then backward, and prepend 0 to the forward half and 1 to the backward half. This reflected binary code is the most common Gray code and can be computed in closed form: to convert an ordinary binary number to its Gray code, simply compute b XOR (b >> 1).
Comments
Loading comments...