1.1 Why do we need codes at all?
Inside a digital system, everything is ultimately a string of 0s and 1s. A code is just a rule that tells us what each pattern of 0s and 1s means. Different jobs need different codes, so a single number like decimal 9 can be represented in several different ways:
| Code | Representation of decimal 9 | Typical use |
|---|---|---|
| Pure binary | 1001 | Arithmetic inside the CPU |
| BCD (8-4-2-1) | 1001 | Driving 7-segment displays |
| Excess-3 | 1100 | Self-complementing arithmetic |
| Gray code | 1101 | Rotary encoders, K-maps |
| ASCII | 0011 1001 | Storing the character '9' |
1.2 The code classification map
1.3 Weighted vs Non-weighted codes
A code is weighted if each bit position has a fixed numeric weight. The decimal value is then Σ (bit × weight).
- 8-4-2-1 BCD: Standard BCD. Weights are 8, 4, 2, 1. So
1001= 8 + 0 + 0 + 1 = 9. - 2-4-2-1 (Aiken) code: Self-complementing; weights chosen so that the 9's-complement of any digit is found by flipping every bit.
A code is non-weighted when bit positions have no fixed numeric weight. The two most important non-weighted codes are:
- Excess-3 code: Formed by adding 3 to the BCD value of the digit. Decimal 0 → 0011, 5 → 1000, 9 → 1100. It is self-complementing, so subtraction can be done by simple complement-and-add.
- Gray code: Successive numbers differ in exactly one bit. This is what makes it perfect for rotary shaft encoders — a small position error never produces a large numerical jump.
1.4 Complete code-conversion table
This is the single most useful table to memorise for Unit I — every code-converter problem in the syllabus reduces to one of these rows.
| Decimal | BCD (8-4-2-1) | Excess-3 | 2-4-2-1 (Aiken) | Gray | Binary |
|---|---|---|---|---|---|
| 0 | 0000 | 0011 | 0000 | 0000 | 0000 |
| 1 | 0001 | 0100 | 0001 | 0001 | 0001 |
| 2 | 0010 | 0101 | 0010 | 0011 | 0010 |
| 3 | 0011 | 0110 | 0011 | 0010 | 0011 |
| 4 | 0100 | 0111 | 0100 | 0110 | 0100 |
| 5 | 0101 | 1000 | 1011 | 0111 | 0101 |
| 6 | 0110 | 1001 | 1100 | 0101 | 0110 |
| 7 | 0111 | 1010 | 1101 | 0100 | 0111 |
| 8 | 1000 | 1011 | 1110 | 1100 | 1000 |
| 9 | 1001 | 1100 | 1111 | 1101 | 1001 |
| 10 | – | – | – | 1111 | 1010 |
| 11 | – | – | – | 1110 | 1011 |
| 12 | – | – | – | 1010 | 1100 |
| 13 | – | – | – | 1011 | 1101 |
| 14 | – | – | – | 1001 | 1110 |
| 15 | – | – | – | 1000 | 1111 |
How to read this: every row shows the same decimal number rendered in five different codes. Notice three things:
- BCD only goes 0000–1001. Codes 1010–1111 in 4-bit BCD are invalid.
- Excess-3 is BCD + 3. Decimal 0 in Excess-3 is 0011 (not 0000), so the all-zero word can be used as a "no digit" signal.
- Gray adjacent rows differ in exactly one bit. Look at the Gray column going 4 → 5 (
0110→0111) or 7 → 8 (0100→1100). Only one bit changes between every pair of consecutive entries — that is the defining property of Gray code.
Binary → Gray conversion
Gray[MSB] = Binary[MSB]
Gray[i] = Binary[i+1] XOR Binary[i]
Example: Binary 1011 → Gray 1 (1⊕0) (0⊕1) (1⊕1) = 1110.
Gray → Binary conversion
Binary[MSB] = Gray[MSB]
Binary[i] = Binary[i+1] XOR Gray[i]
1.5 Alphanumeric codes
When the message contains letters, punctuation and control signals — not just numbers — we need a longer code:
| Code | Bits per character | Capacity | Notes |
|---|---|---|---|
| ASCII | 7 (often stored as 8) | 128 chars | Standard for English text and serial communication |
| Extended ASCII | 8 | 256 chars | Adds graphic and accented characters |
| EBCDIC | 8 | 256 chars | IBM mainframes |
| Unicode (UTF-8) | 8 to 32 (variable) | 1.1 million+ | Modern web/text storage; superset of ASCII |
1.6 Why errors happen
Inside any real digital system, bits are not perfect logical 0s and 1s. They are voltage levels travelling down a wire — and noise, EMI, ground bounce or a marginal supply can flip a 0 into a 1 or vice versa. We classify these flips as:
- Single-bit error: Exactly one bit is wrong (most common in short transmissions).
- Burst error: A contiguous group of bits is corrupted (typical on radio links or scratched disks).
1.7 Error-detecting codes — parity
A parity bit is one extra bit added to a word so the total number of 1s is always even (even parity) or always odd (odd parity).
| Original word | Even parity bit | Transmitted |
|---|---|---|
| 1011001 (four 1s) | 0 | 10110010 |
| 1010100 (three 1s) | 1 | 10101001 |
| 1111111 (seven 1s) | 1 | 11111111 |
| 0000000 (zero 1s) | 0 | 00000000 |
Properties.
- Detects any odd number of bit errors (1, 3, 5 …) — these always change the parity.
- Cannot detect an even number of errors — two flips cancel each other.
- Cannot locate the bad bit; you only know a word is bad.
Parity generator / checker circuit
The same XOR tree is used at both ends — generator side computes the parity bit; receiver side recomputes parity over data and the received parity bit. A non-zero result means at least one bit flipped during transmission.
1.8 Error-correcting codes — Hamming code
To correct a bit error (not just detect it), we need redundancy. The Hamming code does this by inserting parity bits at bit positions 1, 2, 4, 8, … (the powers of two). Each parity bit covers a specific group of data bits.
Encoding a 4-bit data word into a (7,4) Hamming code
Position layout:
Bit position : 1 2 3 4 5 6 7
Contents : P1 P2 D1 P3 D2 D3 D4
Each parity bit Pk is computed over every position whose binary index has the bit k set:
| Parity | Covers positions | Coverage in binary |
|---|---|---|
| P1 | 1, 3, 5, 7 | All positions with bit-0 of index = 1 |
| P2 | 2, 3, 6, 7 | All positions with bit-1 of index = 1 |
| P3 | 4, 5, 6, 7 | All positions with bit-2 of index = 1 |
Set each Pk so its group has even parity.
Worked example — encode the data word D = 1011
We have D1 = 1, D2 = 0, D3 = 1, D4 = 1.
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Contents | P1 | P2 | D1 = 1 | P3 | D2 = 0 | D3 = 1 | D4 = 1 |
- P1 covers positions 1, 3, 5, 7 → bits at 3, 5, 7 = 1, 0, 1 → sum = 2 → P1 = 0 (already even).
- P2 covers positions 2, 3, 6, 7 → bits at 3, 6, 7 = 1, 1, 1 → sum = 3 → P2 = 1 (make even).
- P3 covers positions 4, 5, 6, 7 → bits at 5, 6, 7 = 0, 1, 1 → sum = 2 → P3 = 0 (already even).
Transmitted codeword: 0 1 1 0 0 1 1.
Decoding — locating an error using the syndrome
At the receiver, recompute each parity check. Treat each "parity failed" result as a 1 and "parity OK" as a 0. The combined bits, read from C3 C2 C1, form a syndrome that is the binary position number of the bad bit.
Syndrome interpretation table
| Syndrome (C3 C2 C1) | Decimal | Meaning |
|---|---|---|
| 000 | 0 | No error |
| 001 | 1 | Bit 1 (P1) is wrong |
| 010 | 2 | Bit 2 (P2) is wrong |
| 011 | 3 | Bit 3 (D1) is wrong |
| 100 | 4 | Bit 4 (P3) is wrong |
| 101 | 5 | Bit 5 (D2) is wrong |
| 110 | 6 | Bit 6 (D3) is wrong |
| 111 | 7 | Bit 7 (D4) is wrong |
The (7,4) Hamming code corrects any single-bit error and detects (but cannot correct) two-bit errors. Extended Hamming codes (8,4) add one overall parity bit and gain SECDED — Single Error Correction, Double Error Detection — which is the scheme used by ECC RAM on servers.
1.9 At-a-glance summary
| Topic | Key idea |
|---|---|
| BCD, Excess-3, Gray | Different rules to encode decimal digits — choose by use case |
| ASCII / Unicode | Alphanumeric codes for text |
| Parity | Adds one bit; detects an odd number of errors |
| Hamming | Adds log₂(n) parity bits; corrects one-bit errors |
| Syndrome | Binary index of the bad bit; 0 means no error |