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 0 to 1 or 1 to 0) 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 like 111 (decimal 7) or 010 (decimal 2), leading to positioning errors.
  • The Gray Code Solution: Transitioning from 3 (010 in Gray) to 4 (110 in 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 ValueStandard BinaryGray Code
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

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
  1. The MSB of the Gray code is identical to the MSB of the binary number:
  2. 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
  1. The MSB of the binary number is identical to the MSB of the Gray code:
  2. 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

  1. 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.
  2. “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.)
  3. “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)YearsMarksSolved in
Define reflected code (inside a multi-part definitions question)2017part of 10§Abstract, §1
Convert into Gray code number (part iii of a 12-mark code-conversion question)2018part of 12below, 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.202410§4

Worked solution — to Gray: convert to binary first, , then apply and :

Binary1001011101
Gray1101110011

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.