BCD e Gray Code
Specialized Binary Encodings
Learn Specialized CodesNot todos binary representations são pure base-2. BCD (Binary-Coded Decimal) e Gray code são specialized encodings esse solve specific problems in digital systems—from displaying numbers on calculators to reading rotary encoders sem errors.
Binary-Coded Decimal (BCD)
O que Is BCD?
BCD represents cada 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 é mais compact but harder to display.
Por que Usar BCD?
Fácil Decimal Display
- Cada 4-bit group → one digit on 7-segment display
- No complex binary-to-decimal conversão needed
- Calculators e meters use este
Exact Decimal Values
- Financial calculations need exact decimal
- 0.10 é exact in BCD, approximate in binary floating-point
- Avoids rounding errors in currency
Human-Readable Storage
- Timestamps often stored in BCD
- Fácil to read in hex dumps
- BIOS real-time clocks use 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) to correct: 1100 + 0110 = 0001 0010 = 12
Por que Add 6?
BCD apenas uses values 0000-1001 (0-9). Values 1010-1111 (10-15) são invalid. Adding 6 carries into o próximo digit.
BCD Variants
Packed BCD
- Two digits per byte
- Mais efficient storage
- Exemplo: 42 = 0100 0010 in one byte
Unpacked BCD
- One digit per byte (upper 4 bits unused/zero)
- Easier processing
- Exemplo: 42 = 00000100 00000010 (two bytes)
Excess-3 BCD
- Add 3 to cada digit before encoding
- Simplifies alguns arithmetic circuits
- 0 = 0011, 9 = 1100
Gray Code
O que Is Gray Code?
Gray code é a binary sequence onde apenas one bit changes entre 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 |
Por que Gray Code Matters
O Problem Gray Code Solves
In regular binary, going from 7 (0111) to 8 (1000) changes 4 bits simultaneously.
- If sensors don't switch at exatamente o mesmo instant
- Momentary false readings occur
- Could read 0000, 0001, 0011, 0111, ou 1111 briefly
Gray Code Solução
- Apenas one bit changes at a time
- No ambiguous intermediate states
- Essential for position encoders
Gray Code Aplicações
Rotary Encoders
- Motor position sensing
- Volume knobs (digital)
- Robotics joint angles
- CNC machine positioning
Error Minimization
- Analog-to-digital conversão
- Genetic algorithms (crossover)
- Karnaugh maps (logic minimization)
Communication
- Alguns error-correcting codes
- Certain modulation schemes
Converting Binary to Gray Code
Method
- Keep o mais significant bit (MSB) o mesmo
- XOR cada bit com o bit to its left
Exemplo: Converter 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 o MSB o mesmo
- XOR cada Gray bit com o previous Binary bit
Exemplo: Converter 1110 (Gray) to Binary
- MSB: 1 (keep)
- 1 XOR 1 = 0
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- Result: 1011 (binary)
Conclusão
BCD e Gray code são specialized binary encodings esse solve specific problems. BCD simplifies decimal display e ensures exact decimal arithmetic—important for calculators e financial systems. Gray code ensures apenas one bit changes entre consecutive values—essential for position encoders e error prevention. While neither é as comum as pure binary, understanding them reveals como diferente number representations serve diferente engenharia needs.