📚 Lesson: Binary Code & Gray Code
1️⃣ Introduction to Number Systems in Digital Logic
Digital systems represent and process information using discrete values — most commonly binary digits (bits), which can be either 0 (LOW) or 1 (HIGH).
Understanding binary and its variations is essential for designing and analyzing digital circuits.
2️⃣ Binary Code (Base-2 System)
Definition
Binary code is a base-2 numeral system that uses only two symbols: 0 and 1.
Each position in a binary number represents a power of 2.
Structure
For an n-bit binary number:
\[(b_{n-1} b_{n-2} \dots b_1 b_0)_2 = b_{n-1} \cdot 2^{n-1} + b_{n-2} \cdot 2^{n-2} + \dots + b_1 \cdot 2^1 + b_0 \cdot 2^0\]Binary Counting
The standard binary count is the sequence in which binary numbers increase by one each time, starting from all zeros.
This sequence is used in timing diagrams, counters, and digital test patterns.
| Decimal | Binary |
|---|---|
| 0 | 0000 |
| 1 | 0001 |
| 2 | 0010 |
| 3 | 0011 |
| 4 | 0100 |
| 5 | 0101 |
| 6 | 0110 |
| 7 | 0111 |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
| 11 | 1011 |
| 12 | 1100 |
| 13 | 1101 |
| 14 | 1110 |
| 15 | 1111 |
Timing Diagram Note
When multiple input bits change at the same time (e.g., from 111 to 000), slight differences in switching
speed can cause a transient glitch — a short, unexpected output change.
Such glitches can cause errors in sensitive circuits, which is one reason alternative codes like Gray Code are used.
3️⃣ Gray Code (Reflected Binary Code)
Definition
Gray Code is a binary numeral system where two successive values differ in only one bit.
It’s also called a Unit Distance Code because the “distance” between consecutive codes is exactly one bit change.
Why Use Gray Code?
- Prevents transient errors during transitions.
- Ideal for rotary encoders, analog-to-digital converters, and mechanical position sensors.
- Ensures only one bit changes at a time, reducing ambiguity.
Gray Code Sequences
2-bit Gray Code: 00 01 11 10
3-bit Gray Code: 000 001 011 010 110 111 101 100
Notice that after the last value, only one bit changes to return to the first value — making it cyclic.
4️⃣ Binary ↔ Gray Code Conversion
Binary to Gray
- MSB of Gray = MSB of Binary.
- Each next Gray bit = XOR of current Binary bit and previous Binary bit.
Example: Binary 1010 → Gray:
-
MSB: 1
-
Next: $1 \oplus 0 = 1$
-
Next: $0 \oplus 1 = 1$
-
Next: $1 \oplus 0 = 1$
Result: 1111
Gray to Binary
- MSB of Binary = MSB of Gray.
- Each next Binary bit = Previous Binary bit XOR Current Gray bit.
Example: Gray 1111 → Binary:
-
MSB: 1
-
Next: $ \oplus 1 = 0$
-
Next: $0 \oplus 1 = 1$
-
Next: $1 \oplus 1 = 0$
Result: 1010
5️⃣ Binary vs. Gray Code (0–15)
| Decimal | 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 |
Teaching Note
-
In binary, multiple bits can change between consecutive numbers (e.g.,
0111→1000changes all four bits). -
In Gray Code, only one bit changes at each step, which is why it’s called a Unit Distance Code.
-
This property is what prevents glitches in timing-sensitive circuits.
6️⃣ Building Gray Code with the Mirroring Method
Another way to generate Gray Code sequences — without doing any bitwise math — is to use the mirroring (or reflect‑and‑prefix) method. This is especially handy for quickly building longer sequences like 4‑bit or 5‑bit Gray codes. Steps:
-
Start with the 1‑bit Gray code: 0 1
-
Mirror the list (write it in reverse order).
-
Prefix the original list with
0and the mirrored list with1. -
Repeat the process to add more bits.
Example: 4‑bit Gray Code
0000 0001 0011 0010 0110 0111 0101 0100 1100 1101 1111 1110 1010 1011 1001 1000
7️⃣ Practice Problems
-
Convert
1101₂to Gray Code. -
Convert Gray Code
1011to Binary. -
Explain why Gray Code is preferred in mechanical position sensors.
-
Identify the glitch-prone transition in the binary count from 0–15.
8️⃣ Summary
- Binary Code is the foundation of all digital logic, representing data in 0s and 1s.
- Standard binary counting is used in timing diagrams but can cause glitches when multiple bits change simultaneously.
- Gray Code changes only one bit at a time, reducing the risk of transient errors.
- Understanding both systems — and converting between them — is essential for digital engineers.