BCD and Gray Code
Specialized Binary Encodings
Learn Specialized CodesNot all binary representations are pure base-2. BCD (Binary-Coded Decimal) and Gray code are specialized encodings that solve specific problems in digital systems—from displaying numbers on calculators to reading rotary encoders without errors.
Binary-Coded Decimal (BCD)
What Is BCD?
BCD represents each decimal digit as a 4-bit binary number:
| Decimal | BCD | Pure Binary |
|---|---|---|
| 0 | 0000 | 0000 |
| 5 | 0101 | 0101 |
| 9 | 1001 | 1001 |
| 10 | 0001 0000 | 1010 |
| 42 | 0100 0010 | 101010 |
| 99 | 1001 1001 | 1100011 |
Key Difference
BCD uses 4 bits per decimal digit; pure binary is more compact but harder to display.
Why Use BCD?
Easy Decimal Display
- Each 4-bit group → one digit on 7-segment display
- No complex binary-to-decimal conversion needed
- Calculators and meters use this
Exact Decimal Values
- Financial calculations need exact decimal
- 0.10 is exact in BCD, approximate in binary floating-point
- Avoids rounding errors in currency
Human-Readable Storage
- Timestamps often stored in BCD
- Easy to read in hex dumps
- BIOS real-time clocks use BCD
BCD Arithmetic
Addition Challenge
BCD addition needs adjustment when result > 9:
- 0101 + 0100 = 1001 (5 + 4 = 9) ✓ Valid BCD
- 0111 + 0101 = 1100 (7 + 5 = 12) ✗ Invalid BCD
- Must add 6 (0110) to correct: 1100 + 0110 = 0001 0010 = 12
Why Add 6?
BCD only uses values 0000-1001 (0-9). Values 1010-1111 (10-15) are invalid. Adding 6 carries into the next digit.
BCD Variants
Packed BCD
- Two digits per byte
- More efficient storage
- Example: 42 = 0100 0010 in one byte
Unpacked BCD
- One digit per byte (upper 4 bits unused/zero)
- Easier processing
- Example: 42 = 00000100 00000010 (two bytes)
Excess-3 BCD
- Add 3 to each digit before encoding
- Simplifies some arithmetic circuits
- 0 = 0011, 9 = 1100
Gray Code
What Is Gray Code?
Gray code is a binary sequence where only one bit changes between consecutive values:
| 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 |
Why Gray Code Matters
The Problem Gray Code Solves
In regular binary, going from 7 (0111) to 8 (1000) changes 4 bits simultaneously.
- If sensors don't switch at exactly the same instant
- Momentary false readings occur
- Could read 0000, 0001, 0011, 0111, or 1111 briefly
Gray Code Solution
- Only one bit changes at a time
- No ambiguous intermediate states
- Essential for position encoders
Gray Code Applications
Rotary Encoders
- Motor position sensing
- Volume knobs (digital)
- Robotics joint angles
- CNC machine positioning
Error Minimization
- Analog-to-digital conversion
- Genetic algorithms (crossover)
- Karnaugh maps (logic minimization)
Communication
- Some error-correcting codes
- Certain modulation schemes
Converting Binary to Gray Code
Method
- Keep the most significant bit (MSB) the same
- XOR each bit with the bit to its left
Example: Convert 1011 (binary) to Gray
- MSB: 1 (keep)
- 1 XOR 0 = 1
- 0 XOR 1 = 1
- 1 XOR 1 = 0
- Result: 1110 (Gray)
Formula
Gray[i] = Binary[i] XOR Binary[i+1]
Gray[MSB] = Binary[MSB]
Converting Gray Code to Binary
Method
- Keep the MSB the same
- XOR each Gray bit with the previous Binary bit
Example: Convert 1110 (Gray) to Binary
- MSB: 1 (keep)
- 1 XOR 1 = 0
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- Result: 1011 (binary)
まとめ
BCD and Gray code are specialized binary encodings that solve specific problems. BCD simplifies decimal display and ensures exact decimal arithmetic—important for calculators and financial systems. Gray code ensures only one bit changes between consecutive values—essential for position encoders and error prevention. While neither is as common as pure binary, understanding them reveals how different number representations serve different engineering needs.