Gray Code — What It Is and How to Convert Binary to Gray Code
Learn what Gray code is, why it's used in digital electronics, and how to convert binary to Gray code and back. Includes complete reference table and worked examples for BCA/BTech exams.
What is Gray Code?
In normal binary counting, multiple bits can change simultaneously — going from 0111 (7) to 1000 (8), all four bits flip. This causes glitches in hardware circuits because bits don’t switch at exactly the same instant. Gray code solves this by ensuring only one bit changes between consecutive numbers.
🔧 Practice conversions: Number System Converter — convert binary numbers instantly, and Binary Arithmetic Calculator for XOR operations.
Gray code (also called reflected binary code) is a binary numeral system where two successive values differ in only one bit position.
Complete 4-Bit Gray Code Table
| Decimal | Binary | Gray Code | Bits Changed |
|---|---|---|---|
| 0 | 0000 | 0000 | — |
| 1 | 0001 | 0001 | 1 bit |
| 2 | 0010 | 0011 | 1 bit |
| 3 | 0011 | 0010 | 1 bit |
| 4 | 0100 | 0110 | 1 bit |
| 5 | 0101 | 0111 | 1 bit |
| 6 | 0110 | 0101 | 1 bit |
| 7 | 0111 | 0100 | 1 bit |
| 8 | 1000 | 1100 | 1 bit |
| 9 | 1001 | 1101 | 1 bit |
| 10 | 1010 | 1111 | 1 bit |
| 11 | 1011 | 1110 | 1 bit |
| 12 | 1100 | 1010 | 1 bit |
| 13 | 1101 | 1011 | 1 bit |
| 14 | 1110 | 1001 | 1 bit |
| 15 | 1111 | 1000 | 1 bit |
Notice: in the Gray column, every adjacent row differs by exactly one bit. In the Binary column, row 7→8 changes all 4 bits — that’s what Gray code prevents.
Binary → Gray Code Conversion
Rules:
- MSB (leftmost bit) stays the same
- Each subsequent Gray bit = XOR of current binary bit with previous binary bit
XOR Truth Table:
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example 1: Convert Binary 1011 to Gray Code
Binary: 1 0 1 1
↓ ↓ ↓ ↓
Gray: 1 1⊕0 0⊕1 1⊕1
= 1 1 1 0
Answer: 1110
Step by step:
- G₃ = B₃ = 1 (MSB stays)
- G₂ = B₃ ⊕ B₂ = 1 ⊕ 0 = 1
- G₁ = B₂ ⊕ B₁ = 0 ⊕ 1 = 1
- G₀ = B₁ ⊕ B₀ = 1 ⊕ 1 = 0
Example 2: Convert Binary 1101 to Gray Code
B: 1 1 0 1
G: 1 0 1 1
G₃ = 1
G₂ = 1⊕1 = 0
G₁ = 1⊕0 = 1
G₀ = 0⊕1 = 1
Answer: 1011
Example 3: Convert Binary 10110 to Gray Code
B: 1 0 1 1 0
G: 1 1 1 0 1
Answer: 11101
Gray Code → Binary Conversion
Rules:
- MSB stays the same
- Each subsequent Binary bit = XOR of current Gray bit with the previous Binary bit (already computed)
Example: Convert Gray 1110 to Binary
Gray: 1 1 1 0
Binary: 1 ? ? ?
B₃ = G₃ = 1
B₂ = G₂ ⊕ B₃ = 1 ⊕ 1 = 0
B₁ = G₁ ⊕ B₂ = 1 ⊕ 0 = 1
B₀ = G₀ ⊕ B₁ = 0 ⊕ 1 = 1
Answer: 1011 ✓ (matches our earlier conversion)
Worked Conversions Summary
| Binary | Gray Code | Verification |
|---|---|---|
| 0000 | 0000 | MSB=0, 0⊕0=0, 0⊕0=0, 0⊕0=0 |
| 0101 | 0111 | 0, 0⊕1=1, 1⊕0=1, 0⊕1=1 |
| 1011 | 1110 | 1, 1⊕0=1, 0⊕1=1, 1⊕1=0 |
| 1100 | 1010 | 1, 1⊕1=0, 1⊕0=1, 0⊕0=0 |
| 1111 | 1000 | 1, 1⊕1=0, 1⊕1=0, 1⊕1=0 |
Why Gray Code is Used
- Rotary encoders — shaft position sensors use Gray code discs to avoid ambiguous readings during transitions
- Karnaugh maps (K-maps) — adjacent cells differ by one variable, which is Gray code ordering
- Error minimization — only 1 bit can be wrong during a transition, limiting errors to ±1
- Analog-to-digital converters — reduces glitches during conversion
- Genetic algorithms — Gray coding prevents large jumps from single-bit mutations
Common Exam Questions
- “Convert binary 10110 to Gray code” (direct application)
- “Convert Gray code 11001 to binary” (reverse application)
- “Why is Gray code used in K-maps?” (theory)
- “Draw 3-bit Gray code sequence” (listing)
- “Design a circuit to convert binary to Gray” (XOR gates)
Related Tools
Related Articles
FAQ
Q: What is the advantage of Gray code over binary? A: Only one bit changes between consecutive numbers, preventing glitches in hardware transitions. In binary, 0111→1000 changes all 4 bits simultaneously — if they don’t switch at the exact same time, intermediate invalid values appear.
Q: How is XOR used in Gray code conversion? A: Binary→Gray: each Gray bit (except MSB) = XOR of adjacent binary bits. Gray→Binary: each binary bit = XOR of Gray bit with previously computed binary bit.
Q: Is Gray code weighted or unweighted? A: Gray code is unweighted — unlike binary where each position has a weight (1, 2, 4, 8…), Gray code positions don’t have fixed mathematical weights. You can’t directly compute decimal value from Gray code without first converting to binary.
Q: How many bits change between Gray code 7 and 8? A: Only 1 bit. Gray(7) = 0100, Gray(8) = 1100 — only the MSB changes. In binary, 0111→1000 changes all 4 bits.
Q: Where do K-maps use Gray code ordering? A: K-map column/row headers use Gray code sequence (00, 01, 11, 10) so that adjacent cells differ by exactly one variable — enabling grouping of minterms.
Free tools
Learn faster with Numverto
Free number system converters, binary tools, EMI calculators, and more, with step-by-step working.
Written by
Numverto Editorial Team
Founder of Numverto. MCA graduate, full-stack developer, and lifelong learner who built this platform so every student gets the explanation, not just the answer.
Related tools
More from Numverto Blog
ieee754
IEEE 754 Floating Point Explained Sign, Exponent & Mantissa
Master IEEE 754 floating point format sign, exponent, mantissa explained with real conversion examples for exams and coding interviews.
13 July 2026
IEEE 754 Standard
Why Does 0.1 + 0.2 Equal 0.30000000000000004? Floating-Point Errors Explained
Understand why computers produce floating-point precision errors, how the IEEE 754 standard stores decimal numbers in binary, why rounding occurs, and the best practices developers use to write accurate, reliable code.
1 July 2026
Discussion