Related Concepts: 04 BCD and Weighted Digital Codes | 05 Excess-3 Code & Self-Complementing Logic | 09 XOR Gate Properties, Parity Generator & Error Checking

06 Gray Code & Code Conversions

What is Gray Code?

Gray Code (frequently termed Reflected Binary Code) is a 4-bit (or -bit) non-weighted binary coding system.

According to the class notes, its defining fundamental property is: “A binary code system that changes ONLY ONE BIT at a time as it proceeds between any two consecutive code numbers.”


1. Why is it Called “Reflected” Code?

The code is named “reflected” because the sequence of code words for the second half of any -bit Gray code sequence is the exact mirror image {reflection} of the first half, with the exception of the Most Significant Bit (MSB), which is inverted from 0 to 1.

2-Bit Gray Code Reflection Example:
Decimal 0:  0 0  ---| First half (MSB = 0)
Decimal 1:  0 1  ---|
-------------------- Reflection Axis
Decimal 2:  1 1  ---| Second half (MSB = 1, LSB reflected: 1 then 0)
Decimal 3:  1 0  ---|

2. Characteristics & Applications

Advantages (Merits)

  • Eliminates Switching Errors & Spurious Transients: In standard binary counting, transitioning from (0111) to (1000) requires 4 bits to change state simultaneously. Due to unequal gate delays, temporary false intermediate output states {glitches} occur. In Gray code, only 1 bit flips (0010 0110), eliminating transient errors.
  • Essential for Shaft Encoders & Position Sensors: Widely used in mechanical rotary encoders, optical position sensors, and analog-to-digital converters (ADCs) to prevent boundary positioning errors.
  • Karnaugh Map (K-Map) Axis Indexing: Used for K-map row/column labels (00, 01, 11, 10) so adjacent cells differ by exactly 1 variable, satisfying adjacency laws.

Disadvantages (Demerits)

  • Non-Weighted Code: Bit positions do not have fixed numerical weights.
  • Unsuitable for Arithmetic: Direct addition, subtraction, or arithmetic manipulation is highly impractical without first converting to pure binary.

3. Conversion Algorithms & Formulas

Conversions between pure Binary and Gray code rely entirely on the Exclusive-OR (XOR, ) operation.

graph TD
    subgraph Binary to Gray Algorithm
        B3[B3 MSB] --> G3[G3 = B3]
        B3 & B2[B2] -->|XOR| G2[G2 = B3 ⊕ B2]
        B2 & B1[B1] -->|XOR| G1[G1 = B2 ⊕ B1]
        B1 & B0[B0 LSB] -->|XOR| G0[G0 = B1 ⊕ B0]
    end
graph TD
    subgraph Gray to Binary Algorithm
        g3[G3 MSB] --> b3[B3 = G3]
        b3 & g2[G2] -->|XOR| b2[B2 = B3 ⊕ G2]
        b2 & g1[G1] -->|XOR| b1[B1 = B2 ⊕ G1]
        b1 & g0[G0 LSB] -->|XOR| b0[B0 = B1 ⊕ G0]
    end

A. Binary to Gray Code Conversion

Given an -bit Binary number (), compute Gray bits ():

  1. MSB Rule: The Most Significant Bit remains unchanged:
  2. Subsequent Bits Rule: XOR the current binary bit with the preceding binary bit:

For a 4-bit number ():


B. Gray Code to Binary Conversion

Given an -bit Gray code number (), compute Binary bits ():

  1. MSB Rule: The Most Significant Bit remains unchanged:
  2. Subsequent Bits Rule: XOR the newly calculated binary bit with the next Gray bit:

For a 4-bit number ():


4. Design: 4-Bit Reflected Code to Binary Converter Circuit

Major Exam Design Problem (2024 - 10 Marks)

Question: Design a combinational logic circuit that converts a 4-bit reflected code (Gray code) number to a 4-bit binary number using XOR gates.

Design Solution:

  1. Inputs: 4-bit Reflected (Gray) Code ().
  2. Outputs: 4-bit Binary Code ().
  3. Boolean Equations:

Circuit Architecture:

The circuit is implemented using a cascading chain of three 2-input XOR gates:

  • passes straight through to output .
  • and enter XOR Gate 1 Output .
  • and enter XOR Gate 2 Output .
  • and enter XOR Gate 3 Output .

Past Year Questions (PYQs)

1. Reflected Code Definition (2016 - 10 Marks Partial)

Question: Define Reflected code.

  • Solution: Write: “A non-weighted binary code system in which adjacent code words differ by only one bit position, eliminating spurious switching errors during state transitions.”

2. Large Decimal to Gray Conversion (2018 - 12 Marks Partial)

Question: Convert the decimal number into a Gray code number.

Solution:

  • Step 1: Convert to pure Binary:
  • Step 2: Convert Binary to Gray Code using XOR:
  • Final Answer: .