BCD y Gray Code
Specialized Binary Encodings
Learn Specialized CodesNot todo binary representations son pure base-2. BCD (Binary-Coded Decimal) y Gray code son especializado encodings eso solve specific problems in digital sistemas—desde displaying numeros on calculators un/una reading rotary encoders without errors.
Binary-Coded Decimal (BCD)
What Is BCD?
BCD represents cada decimal digit as un/una 4-bit binary numero:
| 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 es mas compact pero harder un/una mostrar.
Why Use BCD?
Easy Decimal Display
- Each 4-bit group → one digit on 7-segment mostrar
- No complex binary-un/una-decimal conversion needed
- Calculators y metros usar esto
Exact Decimal Values
- Financial calculos necesitar exacto decimal
- 0.10 es exacto in BCD, approximate in binary floating-point
- Avoids rounding errors in currency
Human-Readable Storage
- Timestamps often stored in BCD
- Easy un/una read in hex dumps
- BIOS real-time clocks usar BCD
BCD Arithmetic
Addition Challenge
BCD addition needs adjustment cuando resultado > 9:
- 0101 + 0100 = 1001 (5 + 4 = 9) ✓ Valid BCD
- 0111 + 0101 = 1100 (7 + 5 = 12) ✗ Invalid BCD
- Must add 6 (0110) un/una correct: 1100 + 0110 = 0001 0010 = 12
Why Suma 6?
BCD solo uses valores 0000-1001 (0-9). Values 1010-1111 (10-15) son invalid. Adding 6 carries into el/la siguiente digit.
BCD Variants
Packed BCD
- Two digits per byte
- More efficient storage
- Ejemplo: 42 = 0100 0010 in one byte
Unpacked BCD
- One digit per byte (upper 4 bits unused/zero)
- Easier processing
- Ejemplo: 42 = 00000100 00000010 (two bytes)
Excess-3 BCD
- Suma 3 un/una cada digit before encoding
- Simplifies algunos arithmetic circuits
- 0 = 0011, 9 = 1100
Gray Code
What Is Gray Code?
Gray code es un/una binary sequence donde solo one bit changes entre consecutive valores:
| 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
El/La Problem Gray Code Solves
In regular binary, going desde 7 (0111) un/una 8 (1000) changes 4 bits simultaneously.
- If sensors don't switch at exactamente el/la mismo instant
- Momentary false readings occur
- Could read 0000, 0001, 0011, 0111, o 1111 briefly
Gray Code Solution
- Only one bit changes at un/una time
- No ambiguous intermediate states
- Essential for position encoders
Gray Code Aplicaciones
Rotary Encoders
- Motor position sensing
- Volumen knobs (digital)
- Robotics joint angles
- CNC machine positioning
Error Minimization
- Analog-un/una-digital conversion
- Genetic algorithms (crossover)
- Karnaugh maps (logic minimization)
Communication
- Some error-correcting codes
- Certain modulation schemes
Converting Binary un/una Gray Code
Method
- Keep el/la la mayoria significant bit (MSB) el/la mismo
- XOR cada bit with el/la bit un/una su left
Ejemplo: Convertir 1011 (binary) un/una 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 un/una Binary
Method
- Keep el/la MSB el/la mismo
- XOR cada Gray bit with el/la previous Binary bit
Ejemplo: Convertir 1110 (Gray) un/una Binary
- MSB: 1 (keep)
- 1 XOR 1 = 0
- 0 XOR 1 = 1
- 1 XOR 0 = 1
- Result: 1011 (binary)
Conclusion
BCD y Gray code son especializado binary encodings eso solve specific problems. BCD simplifies decimal mostrar y ensures exacto decimal arithmetic—importante for calculators y financial sistemas. Gray code ensures solo one bit changes entre consecutive valores—esencial for position encoders y error prevention. While neither es as comun as pure binary, understanding them reveals como diferente numero representations serve diferente engineering needs.