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