Skip to main content
Numverto numverto
binary gray code digital electronics

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.

Numverto Editorial Team Numverto Editorial Team 6 min read Editorial standards

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

DecimalBinaryGray CodeBits Changed
000000000
1000100011 bit
2001000111 bit
3001100101 bit
4010001101 bit
5010101111 bit
6011001011 bit
7011101001 bit
8100011001 bit
9100111011 bit
10101011111 bit
11101111101 bit
12110010101 bit
13110110111 bit
14111010011 bit
15111110001 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:

  1. MSB (leftmost bit) stays the same
  2. Each subsequent Gray bit = XOR of current binary bit with previous binary bit

XOR Truth Table:

ABA XOR B
000
011
101
110

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:

  1. MSB stays the same
  2. 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

BinaryGray CodeVerification
00000000MSB=0, 0⊕0=0, 0⊕0=0, 0⊕0=0
010101110, 0⊕1=1, 1⊕0=1, 0⊕1=1
101111101, 1⊕0=1, 0⊕1=1, 1⊕1=0
110010101, 1⊕1=0, 1⊕0=1, 0⊕0=0
111110001, 1⊕1=0, 1⊕1=0, 1⊕1=0

Why Gray Code is Used

  1. Rotary encoders — shaft position sensors use Gray code discs to avoid ambiguous readings during transitions
  2. Karnaugh maps (K-maps) — adjacent cells differ by one variable, which is Gray code ordering
  3. Error minimization — only 1 bit can be wrong during a transition, limiting errors to ±1
  4. Analog-to-digital converters — reduces glitches during conversion
  5. 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)

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.

Advertisement
Share: Twitter LinkedIn Facebook

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

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

Advertisement

More from Numverto Blog

Discussion

Comments

Popular Tools

View all 25 free tools → · Read tutorials · Number system guide