2.11 Gray Code: Single-Bit-Transition Encoding
Convert binary to Gray code using the XOR rule, and explain why the single-bit-transition property makes Gray code preferable to pure binary in physical position-sensing applications.
Gray code is a way of ordering binary bit patterns so that any two numerically adjacent values differ in exactly one bit position, unlike ordinary binary counting where multiple bits can change simultaneously between adjacent values. This single-bit-transition property is not a mathematical curiosity — it directly solves a real physical hardware problem: when a physical sensor (like a rotary encoder) reads multiple bits that are supposed to change together, imperfect timing can make them appear to change at slightly different instants, and Gray code guarantees that any such misread never produces a wildly wrong intermediate value.
Gray code is not counting order
It is essential to understand from the start that Gray code is not ordinary binary counting — its main property is that adjacent values differ by exactly one bit, and this property, not numeric counting order in the usual sense, is what defines the code. If you list out consecutive Gray code values, they do not represent the pure-binary sequence 0, 1, 2, 3, ... in the sense of matching binary bit patterns to the same numbers; rather, they form a reflected, reordered sequence of bit patterns chosen specifically so consecutive entries differ by one bit.
This distinction matters because a beginner might assume Gray code is 'just another way to write numbers' with the same arithmetic properties as binary — but Gray code is not directly used for arithmetic; a Gray-coded value typically must be converted back to ordinary binary before being added, subtracted, or compared numerically in the usual sense.
Binary-to-Gray conversion via XOR
Converting an ordinary binary number to its Gray code equivalent uses a simple bit-by-bit rule: the Gray code's most significant bit equals the binary number's most significant bit unchanged, and every other Gray code bit equals the XOR (exclusive-OR) of that binary bit with the binary bit immediately to its left (the next more significant position).
This XOR-based construction is what guarantees the single-bit-transition property: because each Gray bit only depends on two adjacent binary bits, incrementing the binary value by 1 (which, in the worst case such as going from 0111 to 1000, flips many binary bits at once) still only flips exactly one bit of the resulting Gray code.
Why this matters: rotary encoders and mechanical sensing
A rotary encoder is a physical device that reports angular position using a multi-bit code read from a rotating disc. If ordinary binary were used and the disc happened to be read at the exact instant several bits were simultaneously transitioning (e.g., from 0111 to 1000), tiny mechanical or electrical timing differences between the individual bit sensors could cause a momentary, wildly incorrect intermediate reading (such as 0000 or 1111) before settling on the correct value — a glitch that could seriously confuse a control system relying on that position.
Gray code eliminates this specific failure mode because, by construction, only one bit ever changes between any two adjacent positions. Even if that single bit's timing is slightly uncertain, the worst-case misread is only ever the previous or next correct value — never a wildly incorrect intermediate one — which is exactly why Gray code is the standard choice for rotary and linear position encoders.
Binary-to-Gray XOR conversion rule
The MSB is always copied unchanged from binary to Gray; every subsequent Gray bit is formed from XOR-ing two adjacent binary bits. Gray-to-binary conversion runs the equivalent process in reverse, propagating XOR from the MSB down through each subsequent bit.
G(MSB) = B(MSB)
G(i) = B(i+1) XOR B(i) for every other bit position i
where:
B = the original binary bit pattern
G = the resulting Gray code bit patternCode Examples
The Gray MSB copies the binary MSB directly: G3 = B3 = 1. Each subsequent Gray bit XORs its binary position with the binary bit to its left: G2 = B3 XOR B2 = 1 XOR 0 = 1; G1 = B2 XOR B1 = 0 XOR 1 = 1; G0 = B1 XOR B0 = 1 XOR 1 = 0. The result, Gray code 1110, differs from the adjacent binary value's Gray code by exactly one bit.
Binary 1011 converts to Gray code 1110
Binary: 1 0 1 1
| | | |
(MSB) XOR XOR XOR
| 1^0 0^1 1^1
v | | |
Gray: 1 1 1 0Single-bit transitions: Gray code vs binary counting
Gray code guarantees exactly one bit changes between any two consecutive values, unlike binary which can flip multiple bits at once.
Decimal Binary Gray
0 000 000
1 001 001
2 010 011
3 011 010
4 100 110
5 101 111
Binary 3->4 flips THREE bits (011->100).
Gray 3->4 flips ONE bit (010->110).Important Rules
Gray code's defining property: any two numerically adjacent values differ in exactly one bit position.
Binary-to-Gray conversion: G(MSB) = B(MSB); G(i) = B(i+1) XOR B(i) for all other positions.
Gray code is not the same sequence as binary counting order in bit-pattern terms — it is a reordering chosen specifically to satisfy the single-bit-transition property.
Gray-coded values are typically converted back to binary before arithmetic; Gray code itself is not used for addition/subtraction directly.
Where this shows up
Beyond rotary encoders (measuring motor shaft position, robotic joint angles, or volume-knob rotation), Gray code sequencing is also used in Karnaugh map cell ordering (Module 5), in certain error-correction and communication schemes to limit the effect of single-bit transmission glitches, and in some low-power counter designs where minimizing simultaneous bit transitions reduces switching noise and power spikes.
Assuming Gray code values can be used interchangeably with ordinary binary counting values in arithmetic, since they 'look like' a sequence of numbers.
Convert Gray code back to standard binary first whenever arithmetic (addition, comparison, etc.) is needed — Gray code's ordering property does not carry the same positional-weight arithmetic meaning as binary.
Gray code is not ordinary binary counting. Its main property is that adjacent values differ by one bit, which is a property about transitions between consecutive values, not a statement that Gray-coded bit patterns can be summed or compared using standard binary positional-weight arithmetic.
[tricky] What is the key defining property of Gray code that distinguishes it from ordinary binary counting? A. Gray code uses fewer bits than binary for the same range of values B. Any two numerically adjacent values in Gray code differ in exactly one bit position C. Gray code can represent negative numbers directly, unlike binary D. Gray code digits are always in reverse order compared to binary
B. Any two numerically adjacent values in Gray code differ in exactly one bit position — Gray code is not ordinary binary counting; its main property is that adjacent values differ by one bit. This is specifically what makes it useful for physical sensing applications where multiple simultaneous bit changes could otherwise cause misreadings.
[interview] Why is Gray code preferred over plain binary for rotary position encoders? A. Gray code sensors are cheaper to manufacture in all cases B. Because only one bit ever changes between adjacent positions, a misread due to imperfect timing between bit sensors can only ever produce the correct previous or next value, never a wildly wrong intermediate one C. Gray code encoders spin faster than binary encoders D. Binary encoders cannot physically be built for rotary applications
B. Because only one bit ever changes between adjacent positions, a misread due to imperfect timing between bit sensors can only ever produce the correct previous or next value, never a wildly wrong intermediate one — In plain binary, transitions like 0111 to 1000 flip multiple bits simultaneously, and if a sensor reads those bits at slightly different instants due to mechanical/electrical timing skew, it can briefly report a completely wrong intermediate value. Gray code's single-bit-transition property bounds this worst case to at most the adjacent correct value.
Convert the binary number 0110 to Gray code using the XOR rule, and verify the single-bit-transition property by also converting the adjacent binary value 0111 and comparing the two Gray results.
For 0110: G3=0, G2=0^1=1, G1=1^1=0, G0=1^0=1 -> Gray 0101. For 0111: G3=0, G2=0^1=1, G1=1^1=0, G0=1^1=0 -> Gray 0100. Comparing 0101 and 0100, only the LSB differs -- confirming the single-bit-transition property even though the binary values 0110 and 0111 differ only in their LSB in this particular case as well.
Apply G(MSB)=B(MSB) and G(i)=B(i+1) XOR B(i) to both binary values, then compare the resulting Gray patterns bit by bit.