Some folks building some machines had a need for a sort of special BCD (binary coded decimal) variation that changed only one single bit in the encoding while stepping (or counting) sequentially up or down. That novel encoding system is called ‘The Gray Code’ and was first described by, surprisingly, a fellow named Gray.

The origin of the design was in mechanical switching, such as relays or rotary wheels. By only changing a single bit while counting, the implementation could avoid some debouncing scenarios and the notion of ‘ready’ latching after the end of propagation. Not to mention, by only flipping one bit, the power requirements for the relays or mechanical encoders was O(1). Imagine if you had a heavy wheel with a big stepper motor, and for example counting from 0x7 (b0111) to 0x8 (b1000) – that’s a 4x, O(n), current requirement for rotation , and a dependency on waiting for all those wheels finally settling into position.

The Wikipedia article on Gray Codes lists a ridiculous number of variations, leaving me to wonder what the differences are but also leaving me too lazy to read them all. As a programmer, I think these smell like the ‘not invented here’ behavior that many of us software types display. Or maybe it was some dumb patent. Or maybe just variations in the applied mechanical circuits, like a pressure measurement device might have its cylinders laid out from 0-9, but an industrial teletype had a different order of digits so that it could optimize printing while avoiding a jam.

Another scan of the variations implies that 99.36%1 of the usages of ‘Gray Code’ refer to basic ‘Binary Reflected Gray Code’ – but then the mathmaticians got hold of the concept and muddied the shit out of that last 0.64%2 of the scenarios. For what it’s worth, the venerable Art of Electronics by Horowitz and Hill just treats it as the basic ‘Gray Code’ and shows a few logic circuits that implement it.


Notes

  1. I lie with statistics. This number is really just a shorthand for saying “Almost Everything.” 

  2. Ditto, inverted, i.e., “Almost Nothing.”