Apex › Data & encoding

Why encoders use Gray code (one bit at a time)

A rotary or optical encoder reads its position as bits, and plain binary flips several bits at a carry boundary (3→4 is 011→100) — so a mistimed read mid-transition can be garbage like 7 or 0. Gray code reorders the values so consecutive ones differ by exactly one bit, so any read is off by at most one. Build it by reflect-and-prefix, or gray(n)=n XOR (n≫1). Verified: every consecutive pair (incl. wrap) differs by one bit. Step the dial and compare binary vs Gray. Used in encoders, clock-domain counters, and Karnaugh maps.

More in Data & encoding