Related Concepts: 2.01 BCD & Weighted Digital Codes | 2.02 Excess-3 Code & Self-Complementing Logic | 2.04 Error Control, Parity Generators & Checkers
2.03 Gray Code & Code Conversions
What is Gray Code?
Gray Code (also called Reflected Binary Code) is an -bit non-weighted binary coding system. Its defining property is that only one bit changes state (from
0to1or1to0) between any two consecutive values.
1. Physical Significance: Why Use Gray Code?
Standard binary numbers are ideal for math, but they cause severe glitches when interfacing with the physical world due to simultaneous multi-bit transitions:
A. Rotary Shaft Encoders (Position Sensing)
Optical sensors read angular position from coded disks:
- The Binary Problem: Transitioning from decimal 3 (
011) to 4 (100) requires three bits to flip at once. Due to microscopic mechanical misalignments, the sensors will not trigger at the exact same instant. For a fraction of a millisecond, the processor might read an erroneous state like111(decimal 7) or010(decimal 2), leading to positioning errors. - The Gray Code Solution: Transitioning from 3 (
010in Gray) to 4 (110in Gray) flips only the MSB. The sensor is physically guaranteed to read either 3 or 4, with no intermediate erroneous states.
B. Low-Power VLSI Design
In high-speed integrated circuits, flipping bus lines charges/discharges parasitic capacitances, consuming dynamic power. By using Gray code, the average number of bit transitions (switching activity) is minimized, lowering power consumption.
2. 4-Bit Gray Code Lookup Table
Notice how the codes reflect. The second half is the mirror image of the first half (excluding the MSB).
| Decimal Value | Standard Binary | Gray Code |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
3. Conversion Algorithms and Flows
Conversions between pure binary and Gray code are performed using the Exclusive-OR (XOR, ) operator.
3.1 Binary-to-Gray Conversion
Binary-to-Gray Conversion Flow:
Binary: B3 -------> B2 -------> B1 -------> B0
| | | |
| XOR XOR XOR
v v v v
Gray: G3 G2 G1 G0- The MSB of the Gray code is identical to the MSB of the binary number:
- Each subsequent Gray bit is found by XORing the corresponding binary bit with the binary bit to its left:
Binary-to-Gray Conversion: Convert Binary to Gray
Result:
3.2 Gray-to-Binary Conversion
Gray-to-Binary Conversion Flow:
Gray: G3 G2 G1 G0
| ^ ^ ^
| | | |
v XOR XOR XOR
Binary: B3 -------> B2 -------> B1 -------> B0- The MSB of the binary number is identical to the MSB of the Gray code:
- Each subsequent binary bit is found by XORing the previously calculated binary bit with the next Gray code bit:
Gray-to-Binary Conversion: Convert Gray to Binary
Result:
4. Hardware Implementation
To design a combinational circuit that converts a 4-bit Gray code input () to a 4-bit Binary output (), we map the Gray-to-Binary recurrence relations:
This maps directly to a cascade of three XOR gates:
graph LR G3[Input G3] --> B3[Output B3] G3 --> XOR1[XOR Gate 1] G2[Input G2] --> XOR1 XOR1 --> B2[Output B2] B2 --> XOR2[XOR Gate 2] G1[Input G1] --> XOR2 XOR2 --> B1[Output B1] B1 --> XOR3[XOR Gate 3] G0[Input G0] --> XOR3 XOR3 --> B0[Output B0]
The three traps in Gray code questions
- Direction confusion. Binary→Gray XORs two input bits (). Gray→Binary XORs the previously computed output bit with the next input bit (). Getting these backwards produces a plausible-looking but wrong answer, and it is the single most common mistake here.
- “Gray code number” means convert the whole value. If the question gives a decimal like , go decimal → binary → Gray. Do not Gray-code each decimal digit separately — that is how BCD and 5211 work, not Gray. (See 2.01 BCD & Weighted Digital Codes §6 for the full 2018 question where both appear side by side.)
- “Reflected code” is just Gray code. The 2017 and 2024 papers both use that name. If the term appears in a definitions question, say non-weighted code in which only one bit changes between consecutive values, and mention the shaft-encoder application for the extra mark.
Past Year Questions (PYQs)
Question (as asked) Years Marks Solved in Define reflected code (inside a multi-part definitions question) 2017 part of 10 §Abstract, §1 Convert into Gray code number (part iii of a 12-mark code-conversion question) 2018 part of 12 below, and 2.01 BCD & Weighted Digital Codes §6 Design a combinational circuit that converts a four-bit reflected code number to a four-bit binary number. Use XOR gates. 2024 10 §4 Worked solution — to Gray: convert to binary first, , then apply and :
Binary 1 0 0 1 0 1 1 1 0 1 Gray 1 1 0 1 1 1 0 0 1 1
Pattern to notice: Gray code is never a whole question on its own — it always arrives as one part of a larger code-conversion or converter-design question. The conversion itself is two lines of XOR, so the marks hinge on getting the direction right and on drawing a clean labelled XOR cascade for the hardware part.