BCD and weighted codes, self-complementing codes, Gray code, Excess-3 arithmetic, parity-based error detection, Hamming error correction, CRC, and alphanumeric codes — ASCII and EBCDIC.
Binary Coded Decimal (BCD) represents each decimal digit separately using a 4-bit binary code. The weights of the four bits are 8, 4, 2, 1 — hence the alternate name 8421 code. Only the 10 combinations 0000–1001 are valid; the remaining six (1010–1111) are illegal codes.
| Decimal | BCD (8421) | Decimal | BCD (8421) |
|---|---|---|---|
| 0 | 0000 | 5 | 0101 |
| 1 | 0001 | 6 | 0110 |
| 2 | 0010 | 7 | 0111 |
| 3 | 0011 | 8 | 1000 |
| 4 | 0100 | 9 | 1001 |
0010 0101 — two separate 4-bit groups for 2 and 5. In pure binary, (25)₁₀ = 00011001. These are different bit patterns for the same number.
In a weighted code, each bit position carries a fixed numerical weight. The decimal value of a codeword is the sum of the weights of the ‘1’ bits. BCD (8421) is the most common, but other weight assignments are also used.
| Decimal | 8421 (BCD) | 2421 | 5421 | Excess-3 |
|---|---|---|---|---|
| 0 | 0000 | 0000 | 0000 | 0011 |
| 1 | 0001 | 0001 | 0001 | 0100 |
| 2 | 0010 | 0010 | 0010 | 0101 |
| 3 | 0011 | 0011 | 0011 | 0110 |
| 4 | 0100 | 0100 | 0100 | 0111 |
| 5 | 0101 | 1011 | 1000 | 1000 |
| 6 | 0110 | 1100 | 1001 | 1001 |
| 7 | 0111 | 1101 | 1010 | 1010 |
| 8 | 1000 | 1110 | 1011 | 1011 |
| 9 | 1001 | 1111 | 1100 | 1100 |
A code is self-complementing if the 9’s complement of a decimal digit is obtained by simply inverting (complementing) all bits of its codeword. The 2421 code and Excess-3 code are self-complementing; BCD (8421) is not.
Self-complementing codes simplify BCD arithmetic because finding the 10’s complement (used in subtraction) requires only a bit inversion followed by adding 1 — the same as 2’s complement binary arithmetic.
The Gray code (also called reflected binary code) is a non-weighted code in which adjacent values differ by only one bit. This property makes it ideal for rotary encoders, analog-to-digital converters, and any application where glitches from simultaneous bit changes cause errors.
A parity bit is an extra bit appended to a codeword to make the total number of 1s either always even (even parity) or always odd (odd parity). Parity can detect any single-bit error but cannot correct it, and cannot reliably detect two-bit errors (since two flips cancel out).
| Data (7 bits) | Even Parity Bit | Odd Parity Bit | Parity bit appended as bit 8 |
|---|---|---|---|
| 1011001 | 1 | 0 | Four 1s → even parity adds 0; odd adds 1 — wait, 4 is already even → even parity bit = 0 |
| 1110101 | 0 | 1 | Five 1s → odd count → even parity adds 1 to make 6; odd parity adds 0 |
| 0000000 | 0 | 1 | Zero 1s → even parity bit = 0; odd parity bit = 1 |
Richard Hamming’s code adds multiple parity bits at specific positions (powers of 2) such that each data bit is covered by a unique combination of parity bits. This allows both detection and correction of any single-bit error.
For m data bits, the number of parity bits p must satisfy: 2ᵖ ≥ m + p + 1
Blue = parity bit positions (1,2,4 = powers of 2). Green = data bit positions (3,5,6,7).
Data bits: D₇=0, D₆=1, D₅=0, D₃=1 — placed at positions 7,6,5,3.
Received: D₇D₆D₅P₄D₃P₂P₁ = 1 1 1 0 1 0 0
0110100BCD addition works like normal binary addition digit by digit, with one correction: if the sum of any pair of BCD digits exceeds 9 (or produces a carry), add 0110 (decimal 6) to that digit to skip over the 6 illegal codes and produce the correct BCD result with a carry.
0110 to that digit and propagate the carry.
Excess-3 (XS-3) encodes each decimal digit as its BCD value plus 3 (binary 0011). It is self-complementing — the 9’s complement is obtained by bit inversion — which simplifies decimal subtraction.
Addition rule:
0011 (+3) to that digit and propagate the carry.0011 (−3) from the result.CRC is a powerful error-detecting code used in data communications (Ethernet, USB, HDLC) and storage. Unlike parity, it can detect burst errors — multiple consecutive bit flips. CRC treats the data as a polynomial and divides it by a generator polynomial using binary (mod-2) division.
Digital systems must handle not just numbers but also letters, punctuation, and control characters. Alphanumeric codes assign a unique binary pattern to each character.
ASCII is a 7-bit code (128 characters) covering control characters, digits 0–9, uppercase A–Z, lowercase a–z, and punctuation. An 8th bit is added for parity in some implementations. ASCII is the most widely used alphanumeric code.
| Character | 7-bit ASCII | Character | 7-bit ASCII |
|---|---|---|---|
| 0 | 011 0000 | A | 100 0001 |
| 1 | 011 0001 | B | 100 0010 |
| 9 | 011 1001 | Z | 101 1010 |
| Space | 010 0000 | a | 110 0001 |
| ! | 010 0001 | z | 111 1010 |
| DEL | 111 1111 | NUL | 000 0000 |
EBCDIC is IBM’s 8-bit code (256 characters) used on IBM mainframes. It has no technical advantage over ASCII but is still found in legacy mainframe systems. Unlike ASCII, the letters A–Z and a–z are not contiguous in EBCDIC, which complicates some string operations.
| Code / Topic | Key Rule or Property |
|---|---|
| BCD (8421) | 4 bits per decimal digit; valid codes: 0000–1001; illegal: 1010–1111 |
| Gray code | Non-weighted; adjacent values differ by exactly 1 bit; used in encoders/ADCs |
| Excess-3 | BCD + 0011; self-complementing (9’s complement = bit inversion) |
| BCD addition | If digit sum > 9 or carry out → add 0110 to that digit |
| Excess-3 addition | Carry: add 0011; No carry: subtract 0011 |
| Even parity bit | Chosen so total 1-count in codeword is even; detects single-bit errors |
| Hamming code | Parity bits at positions 1,2,4,8,…; syndrome = XOR checks; identifies error position |
| Hamming p bits needed | 2ᵖ ≥ m + p + 1 where m = data bits |
| CRC | Append (n−1) zeros; divide by G(x) mod-2; remainder = CRC; receiver divides frame → remainder should be 0 |
| ASCII | 7-bit; 128 chars; digits at 0110000; A at 1000001; a at 1100001 |
| EBCDIC | 8-bit IBM code; 256 chars; letters not contiguous; legacy mainframes |
| Binary→Gray | MSB same; each Gray bit = XOR of current and previous binary bit |
| Gray→Binary | MSB same; each binary bit = XOR of previous binary bit and current Gray bit |