
Binary Codes
Different ways to map information — digits, letters, states — onto bit patterns.
Description
Agreed mappings between real-world symbols and binary patterns. Raw binary is just numbers; codes give those numbers meaning (a letter, a digit, a state). Define a table mapping each symbol to a unique bit pattern with useful properties.
- Each decimal digit is encoded in its own 4-bit group (0000–1001).
- Easy decimal display, but wastes 6 of 16 codes per digit.
- Consecutive values differ in exactly one bit.
- Prevents multi-bit glitches in encoders and K-map adjacency.
- ASCII encodes characters in 7 bits (128 symbols).
- Unicode extends this to cover all writing systems.
- What: Agreed mappings between real-world symbols and binary patterns.
- Why: Raw binary is just numbers; codes give those numbers meaning (a letter, a digit, a state).
- How: Define a table mapping each symbol to a unique bit pattern with useful properties.
- Where: Keyboards/displays (ASCII), digital meters (BCD), rotary encoders (Gray code).
At a glance
What
Agreed mappings between real-world symbols and binary patterns.
Why
Raw binary is just numbers; codes give those numbers meaning (a letter, a digit, a state).
How
Define a table mapping each symbol to a unique bit pattern with useful properties.
Where
Keyboards/displays (ASCII), digital meters (BCD), rotary encoders (Gray code).
When
Whenever non-numeric or specially-structured data must be stored in bits.
Think of it like…
Codes are like different alphabets for the same idea: BCD spells each decimal digit separately (like writing numbers digit-by-digit), Gray code is a careful walk where you change one footstep at a time so you never trip.
BCD (binary-coded decimal)
- Each decimal digit is encoded in its own 4-bit group (0000–1001).
- Easy decimal display, but wastes 6 of 16 codes per digit.
Gray code
- Consecutive values differ in exactly one bit.
- Prevents multi-bit glitches in encoders and K-map adjacency.
ASCII / Unicode
- ASCII encodes characters in 7 bits (128 symbols).
- Unicode extends this to cover all writing systems.
Decimal · BCD · Gray (0–7)
| Decimal | Binary | BCD | Gray |
|---|---|---|---|
| 0 | 000 | 0000 | 000 |
| 1 | 001 | 0001 | 001 |
| 2 | 010 | 0010 | 011 |
| 3 | 011 | 0011 | 010 |
| 4 | 100 | 0100 | 110 |
| 5 | 101 | 0101 | 111 |
| 6 | 110 | 0110 | 101 |
| 7 | 111 | 0111 | 100 |
Note how each Gray row differs from its neighbor in a single bit.
The 5 Whys
- 1
Why have multiple codes? Different jobs need different bit-pattern properties.
- 2
Why BCD? To display decimal digits without binary-to-decimal conversion.
- 3
Why Gray code? To avoid transient wrong values when several bits change at once.
- 4
Why ASCII? To agree on one mapping so text is portable between machines.
- 5
Root cause: encoding choice shapes error behavior, hardware cost, and interoperability.
Cheat sheet
Working principle
- Define a table mapping each symbol to a unique bit pattern with useful properties.
- Agreed mappings between real-world symbols and binary patterns.
Key facts
- Each decimal digit is encoded in its own 4-bit group (0000–1001).
- Consecutive values differ in exactly one bit.
- ASCII encodes characters in 7 bits (128 symbols).
Why it exists
- Root cause: encoding choice shapes error behavior, hardware cost, and interoperability.