---
title: "Gray code with examples"
subject: "Digital Logic"
chapter: 1
key: "semester_3/digital_logic:gray-code-explain"
source: https://www.bctengineer.com/q/semester_3/digital_logic/gray-code-explain
site: BCT Engineering
---

# Gray code with examples

Digital Logic · Chapter 1: Introduction

- Asked in: 2078 Bhadra, 1.5 marks; 2076 Chaitra, 3 marks; 2074 Chaitra, 2 marks (asked 3 times)
- Video: [Gray Code Explained | Gray code to Binary and Binary to Gray code Conversion](https://www.youtube.com/watch?v=MsMkiTcc_w0) — ALL ABOUT ELECTRONICS
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/digital_logic/gray-code-explain
- Whole chapter: https://www.bctengineer.com/md/semester_3/digital_logic/chapter-1

## Question

Explain Gray code with suitable 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.

## Answer

**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

| Decimal | Binary | Gray | Decimal | Binary | Gray |
|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 8 | 1000 | 1100 |
| 1 | 0001 | 0001 | 9 | 1001 | 1101 |
| 2 | 0010 | 0011 | 10 | 1010 | 1111 |
| 3 | 0011 | 0010 | 11 | 1011 | 1110 |
| 4 | 0100 | 0110 | 12 | 1100 | 1010 |
| 5 | 0101 | 0111 | 13 | 1101 | 1011 |
| 6 | 0110 | 0101 | 14 | 1110 | 1001 |
| 7 | 0111 | 0100 | 15 | 1111 | 1000 |

#### Binary to Gray
MSB stays the same; each next Gray bit = XOR of the current and previous binary bits: $G_i = B_{i+1} \oplus B_i$.

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

#### Gray to binary
MSB stays the same; each next binary bit = previous binary bit XOR current Gray bit: $B_i = B_{i+1} \oplus G_i$.

Example: $(1110)_{Gray}$ → B = 1, 1⊕1 = 0, 0⊕1 = 1, 1⊕0 = 1 → $(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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the defining property of Gray code?
   A. Successive numbers differ in exactly one bit
   B. Each bit has weights 8, 4, 2, 1
   C. Each decimal digit is coded as digit + 3
   D. Successive numbers differ in exactly two bits
   Answer: A. Gray code is a unit-distance code: consecutive values differ in only one bit. (Key point: Gray code definition)

2. What is (1011) binary in Gray code?
   A. 1101
   B. 1110
   C. 1010
   D. 0110
   Answer: B. Keep MSB 1; 1^0=1; 0^1=1; 1^1=0 gives 1110. (Key point: Binary to Gray)

3. Why is Gray code used in rotary shaft encoders?
   A. It allows easy arithmetic
   B. It uses fewer bits than binary
   C. Only one bit changes per step, so no false reading occurs at transitions
   D. It has weights for each bit
   Answer: C. With one-bit changes, a misaligned reading is at most one step off. (Key point: Uses)

## Related in Digital Logic

- [Digital signal and Gray code](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Adigital-signal-gray-code) — Digital Logic, chapter 1, asked 2×
- [Digital IC signal levels and Gray code](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Aic-signal-levels-gray-code) — Digital Logic, chapter 1, asked 2×
- [3-bit Gray code synchronous counter](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Agray-code-sync-counter-3bit) — Digital Logic, chapter 8, asked 2×
- [BCD code and 3-bit binary-to-Gray converter](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Abcd-code-gray-converter) — Digital Logic, chapter 1
- [Gray code and 3-bit binary-to-Gray converter](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Agray-code-binary-gray-converter) — Digital Logic, chapter 1
- [Gray vs binary code, convert (10110)₂ to Gray](https://www.bctengineer.com/question.md?key=semester_3%2Fdigital_logic%3Agray-vs-binary-convert-10110) — Digital Logic, chapter 1
