Related Concepts: 01 BCD & Weighted Digital Codes | 02 Excess-3 Code & Self-Complementing Logic | 04 Error Control, Parity Generators & Checkers
2.03 Gray Code & Code Conversions
What is Gray Code?
Gray Code {also called Reflected Binary Code} is a 4-bit (or -bit) non-weighted binary coding system.
Its defining property is: “A binary coding system in which ONLY ONE BIT changes at a time between any two consecutive code words.”
1. What is Gray Code?
[concept] Concept: Gray Code Definition Gray Code (also known as Reflected Binary Code) is a non-weighted code. Its defining characteristic is that only one bit changes state (0 to 1 or 1 to 0) when transitioning between any two consecutive values.
For example:
- Decimal 1 to 2 in Binary:
0110(2 bits change simultaneously)- Decimal 1 to 2 in Gray:
0111(Only 1 bit changes - the MSB)
2. Why do we use Gray Code? (Physical Systems)
Unlike numerical computations which favor binary numbers, physical interface systems use Gray code to prevent reading errors:
A. Rotary Shaft Encoders (Robotics & Position Sensing)
Imagine a rotating disk divided into angular sectors with optical sensors reading binary codes.
- If standard binary is used and the sensor rotates from decimal 3 (
011) to decimal 4 (100):- Three bits must flip simultaneously.
- Because of tiny physical misalignments, the sensors will not trigger at the exact same instant.
- For a fraction of a millisecond, the controller might read an intermediate state like
111(decimal 7) or010(decimal 2), causing the system to register erratic movement.
- Using Gray code, transitioning from 3 (
010) to 4 (110) flips only the MSB. No intermediate state is ever read.
B. Low-Power VLSI (Digital Integrated Circuits)
In high-speed data buses, switching a bit from 0 to 1 or 1 to 0 requires charging/discharging parasitic capacitance, which consumes dynamic power (). By encoding bus values in Gray code, the average number of transitions per step is minimized, significantly lowering power consumption.
3. 4-Bit Gray Code Lookup Table
| 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 |
4. 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 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 ---|
5. Conversion Algorithms
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 ():
- MSB Rule:
- Subsequent Bits Rule: for .
B. Gray Code to Binary Conversion
Given an -bit Gray code (), compute Binary bits ():
- MSB Rule:
- Subsequent Bits Rule: for .
6. 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.
Circuit Equations & Architecture:
The circuit consists of a cascading chain of three 2-input XOR gates:
- passes straight to output .
- .
- .
- .
Past Year Questions (PYQs)
- [PYQ 2017]: Define Reflected code. (04 Marks)
- [PYQ 2018]: Convert into Gray code number. (Binary: ). (12 Marks)
- [PYQ 2024]: Design a 4-bit reflected code to binary converter circuit using XOR gates. (10 Marks)