Gray Code
Reflected binary code, binary to Gray conversion.
Gray code is everywhere inside rotary encoders and shaft position sensors — it eliminates reading errors at bit boundaries. Every time your motor controller reads angular position, Gray code prevents the glitch that binary would produce.
Core Concept
Gray code is a unit distance code — consecutive values differ by exactly one bit. Standard binary changes multiple bits at once (e.g., 0111 to 1000 changes four bits). During that transition, intermediate invalid states cause glitches in digital circuits reading the bus.
The conversion is performed using XOR gates. A 4-bit binary-to-Gray converter needs only three 74HC86 quad XOR gates. The MSB of Gray equals the MSB of binary; each lower Gray bit is the XOR of the corresponding binary bit with the one above it.
The 74LS348 encodes priority inputs to a 3-bit Gray-coded output with propagation delay around 20 ns at 5 V supply. Fan-out is 20 LS-TTL loads. In CMOS, the CD4532 does the same function with only 0.1 mW quiescent power at 5 V.
Boolean Expression
The conversion from n-bit binary B to Gray G follows: G[n-1] = B[n-1]; G[i] = B[i+1] XOR B[i]. The reverse, Gray to binary, is: B[n-1] = G[n-1]; B[i] = B[i+1] XOR G[i]. The XOR cascade means decoding is sequential from MSB to LSB — you cannot parallelize the reverse conversion without additional logic.
Given:
Binary input B = 1011 (decimal 11)
Formula / Rule:
G3 = B3
G2 = B3 XOR B2
G1 = B2 XOR B1
G0 = B1 XOR B0
Step by step:
G3 = B3 = 1
G2 = B3 XOR B2 = 1 XOR 0 = 1
G1 = B2 XOR B1 = 0 XOR 1 = 1
G0 = B1 XOR B0 = 1 XOR 1 = 0
Final Answer:
Gray code of 1011 = 1110
Verification: consecutive Gray values 1010 and 1110 differ by exactly 1 bit (G1).Exam Tip: GATE often asks you to convert binary to Gray and back in the same problem. Remember that Gray-to-binary conversion is not symmetric — you must propagate from MSB to LSB one bit at a time. A common mistake is applying the binary-to-Gray XOR formula in reverse and getting the wrong answer. Also watch for questions asking which code is 'error-detecting' versus 'unit-distance' — Gray code is unit-distance but not error-detecting on its own.
Key Properties
- Only 1 bit changes between adjacent codewords — eliminates transition glitches in encoders.
- 4-bit Gray code: 74HC86 three-gate chain, propagation delay 6 ns typical at 5 V.
- CD4532 CMOS priority encoder: supply range 3 V to 15 V, quiescent power 0.1 mW at 5 V.
- Fan-out of 74LS family: 20 standard LS-TTL loads; HC family: up to 50 CMOS loads.
- Not a weighted code — position weights are not fixed, so arithmetic in Gray code is impractical.
- Used in Karnaugh maps — adjacent cells differ by 1 bit because K-map axes are Gray-coded.
- The code is cyclic: the last codeword and first codeword also differ by exactly 1 bit.
Quick Revision
- Gray code = reflected binary code, unit distance, cyclic property.
- Binary to Gray: MSB same; each lower bit = XOR of adjacent binary bits (B[i+1] XOR B[i]).
- Gray to binary: MSB same; B[i] = B[i+1] XOR G[i], propagate downward.
- K-map cell ordering uses Gray code to keep adjacent cells differing by 1 variable.
- IC: 74HC86 for conversion; CD4532 for Gray-output priority encoding.
- Not self-complementing, not weighted, not BCD — it encodes only position.
- Exam trap: applying Gray-to-binary conversion by the same XOR rule as binary-to-Gray gives a wrong answer because the reverse cascade uses the already-decoded binary bits, not the Gray bits.
Gray Code Quiz
Test your ability to convert between binary and Gray code and apply the reflected binary property.
Q1.What is the Gray code equivalent of the binary number 1011?
Related Articles
Hamming Code
Error correction, redundant bits, syndrome calculation.
8 min read
Code Converter Circuits
BCD to Gray, Gray to BCD, BCD to Excess-3 circuits.
9 min read
ASCII Code
7-bit character encoding, printable and control characters.
11 min read
Binary Number System
Base-2, place values, binary to decimal conversion.
9 min read
Number System Conversions
Direct methods between binary, octal, decimal, hex.
8 min read