Skip to main content

Gray code with examples

Short answer

Gray code is an unweighted binary code where successive values differ by only one bit. Example: decimal 0–3 in 3-bit Gray are 000, 001, 011, 010. Conversion keeps MSB and each next Gray bit equals XOR of adjacent binary bits; used in encoders to prevent false readings.

Explain Gray code with suitable examples.

Gray code is an unweighted binary code in which two successive numbers differ in only one bit. It is also called the reflected binary code or a unit-distance code.

4-bit Gray code table

DecimalBinaryGrayDecimalBinaryGray
000000000810001100
100010001910011101
2001000111010101111
3001100101110111110
4010001101211001010
5010101111311011011
6011001011411101001
7011101001511111000

Binary to Gray

MSB stays the same; each next Gray bit = XOR of the current and previous binary bits: Gi=Bi+1⊕BiG_i = B_{i+1} \oplus B_i.

Example: (1011)2(1011)_2 → G = 1, 1⊕0 = 1, 0⊕1 = 1, 1⊕1 = 0 → (1110)Gray(1110)_{Gray}.

Gray to binary

MSB stays the same; each next binary bit = previous binary bit XOR current Gray bit: Bi=Bi+1⊕GiB_i = B_{i+1} \oplus G_i.

Example: (1110)Gray(1110)_{Gray} → B = 1, 1⊕1 = 0, 0⊕1 = 1, 1⊕0 = 1 → (1011)2(1011)_2.

Uses

  • Shaft position (rotary) encoders: only one bit changes per step, so no false reading at transitions.
  • K-map labelling (00, 01, 11, 10).
  • Reducing switching errors in counters and ADCs.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Digital Logic chapter 1 questions →