DE-02: Binary Codes — VLSI Trainers
Digital Electronics Series · DE-02

Binary Codes

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.

🔢 BCD (8421) Code

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.

DecimalBCD (8421)DecimalBCD (8421)
0000050101
1000160110
2001070111
3001181000
4010091001
BCD is not straight binary. (25)₁₀ in BCD is 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.

⚖️ Weighted Codes

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.

Decimal8421 (BCD)24215421Excess-3
00000000000000011
10001000100010100
20010001000100101
30011001100110110
40100010001000111
50101101110001000
60110110010011001
70111110110101010
81000111010111011
91001111111001100

🔄 Self-Complementing Codes

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.

⚙️ Gray (Cyclic) Code

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.

Binary vs Gray Code — Adjacent Transitions Decimal Binary Gray Code Bits changed (Gray) 0 000 000 1 001 001 1 bit changed ✓ 2 010 011 1 bit changed ✓ 3 011 010 1 bit changed ✓ 2 bits change between 1↔2
Figure 6 — Binary vs Gray code for 0–3. In binary, going from 1 (001) to 2 (010) changes two bits simultaneously — creating a glitch window where the output momentarily shows 0 or 3. Gray code avoids this entirely by changing only one bit at each step.

Binary → Gray Code Conversion

  1. The MSB of the Gray code is the same as the MSB of the binary number.
  2. Each subsequent Gray bit = XOR of the current binary bit and the previous binary bit.
Example — Convert (1011)₂ to Gray Code
G₃= B₃ = 1
G₂= B₃ ⊕ B₂ = 1 ⊕ 0 = 1
G₁= B₂ ⊕ B₁ = 0 ⊕ 1 = 1
G₀= B₁ ⊕ B₀ = 1 ⊕ 1 = 0
(1011)₂ → Gray Code = (1110)

Gray Code → Binary Conversion

  1. The MSB of binary = MSB of Gray code.
  2. Each subsequent binary bit = XOR of the previous binary bit and the current Gray bit.

🔍 Parity — Error Detection

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 BitOdd Parity BitParity bit appended as bit 8
101100110Four 1s → even parity adds 0; odd adds 1 — wait, 4 is already even → even parity bit = 0
111010101Five 1s → odd count → even parity adds 1 to make 6; odd parity adds 0
000000001Zero 1s → even parity bit = 0; odd parity bit = 1
Limitation of parity. If two bits flip in transit, the parity count is still correct and the error goes undetected. Parity also cannot tell you which bit is wrong — only that there is an error. For correction you need Hamming code (next section) or for burst errors, CRC.

🛡️ Hamming Code — Single-Error Correction

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.

How many parity bits do you need?

For m data bits, the number of parity bits p must satisfy: 2ᵖ ≥ m + p + 1

Parity bit positions in a 7-bit Hamming code

Pos 1
Pos 2
Pos 3
Pos 4
Pos 5
Pos 6
Pos 7
P₁
P₂
D₃
P₄
D₅
D₆
D₇

Blue = parity bit positions (1,2,4 = powers of 2). Green = data bit positions (3,5,6,7).

Parity coverage

Example 2.5 — Construct 7-bit Hamming Code for data 0101 (D₇D₆D₅D₃ = 0101), even parity

Data bits: D₇=0, D₆=1, D₅=0, D₃=1 — placed at positions 7,6,5,3.

P₁Covers pos 1,3,5,7 → bits are P₁, D₃=1, D₅=0, D₇=0. Sum of data bits = 1 (odd) → P₁=1 for even parity
P₂Covers pos 2,3,6,7 → bits are P₂, D₃=1, D₆=1, D₇=0. Sum = 2 (even) → P₂=0
P₄Covers pos 4,5,6,7 → bits are P₄, D₅=0, D₆=1, D₇=0. Sum = 1 (odd) → P₄=1
Hamming codeword: P₁ P₂ D₃ P₄ D₅ D₆ D₇ = 1 0 1 1 0 1 0 = 1011010
Example 2.6 — Error Detection and Correction in received code 1110100

Received: D₇D₆D₅P₄D₃P₂P₁ = 1 1 1 0 1 0 0

C₁Check P₁ (pos 1,3,5,7): bits are 0,1,1,1 → sum=3, odd → C₁=1 (parity fails)
C₂Check P₂ (pos 2,3,6,7): bits are 0,1,1,1 → sum=3, odd → C₂=1 (parity fails)
C₃Check P₄ (pos 4,5,6,7): bits are 0,1,1,1 → sum=3, odd → C₃=1 (parity fails)
SyndromeC₃C₂C₁ = 111 in binary = 7 → error is at position 7
CorrectPosition 7 bit was 1 → flip to 0. Corrected codeword: 0110100
Error at bit position 7. Corrected Hamming code: 0110100

BCD Addition

BCD 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.

Example 2.7 — Add 8765 and 7043 in BCD
BCD of 87651000 0111 0110 0101
BCD of 70430111 0000 0100 0011
Binary sum1111 0111 1010 1000
Check each digit1111=15 >9 → add 6 ✓ | 0111=7 ≤9 → no add | 1010=10 >9 → add 6 ✓ | 1000=8 ≤9 → no add
Add 01101111+0110=10101 → carry | 1010+0110=10000 → carry
8765 + 7043 = 15808 → BCD: 0001 0101 1000 0000 1000
Rule summary. After each 4-bit BCD addition: (1) if result > 9 (i.e., binary 1010 through 1111), or (2) if a carry is generated to the next digit — add 0110 to that digit and propagate the carry.

3️⃣ Excess-3 Addition

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:

Example — Add 45 + 38 using Excess-3
XS-3 of 40111
XS-3 of 51000
XS-3 of 30110
XS-3 of 81011
LSD: 5+8=131000 + 1011 = 1_0011 → carry → add 0011 → 0011+0011=0110 (XS-3 for 3)
MSD: 4+3+1=80111 + 0110 + 1(carry) = 1110 → no carry → subtract 0011 → 1011 (XS-3 for 8)
45 + 38 = 83 → XS-3: 1011 0110

🔁 CRC — Cyclic Redundancy Check

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.

CRC Protocol — Sender and Receiver SENDER 1. Append (n−1) zeros to message M, where n = degree of G(x) 2. Divide augmented M by generator G(x) using mod-2 division 3. Remainder R = CRC bits (n−1 bits wide) 4. Replace the appended zeros with R — transmit M+R Transmitted frame = Message + CRC remainder RECEIVER 1. Receive the frame (Message + CRC) 2. Divide entire received frame by same G(x) 3. If remainder = 0 → no error detected 4. If remainder ≠ 0 → error detected, request retransmit Zero remainder = frame received correctly
Figure 7 — CRC protocol. Both sender and receiver use the same generator polynomial G(x). The sender computes the remainder and appends it; the receiver divides the entire frame and checks for a zero remainder. A non-zero remainder indicates transmission errors.
Why CRC detects burst errors. A burst error of length ≤ (n−1) bits — where n is the degree of the generator polynomial — will always produce a non-zero remainder. With a 16-bit CRC (G(x) of degree 16), all burst errors of 16 bits or fewer are guaranteed to be detected, and longer bursts are detected with probability >99.997%.

🔤 Alphanumeric Codes — ASCII and EBCDIC

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 — American Standard Code for Information Interchange

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.

Character7-bit ASCIICharacter7-bit ASCII
0011 0000A100 0001
1011 0001B100 0010
9011 1001Z101 1010
Space010 0000a110 0001
!010 0001z111 1010
DEL111 1111NUL000 0000
ASCII pattern shortcuts. Digits ‘0’–’9′ start at 0110000 (48 decimal). Uppercase letters start at 1000001 (65). Lowercase letters start at 1100001 (97). The only difference between a capital letter and its lowercase is bit 5 — flipping bit 5 toggles case. This makes case conversion trivial in hardware.

EBCDIC — Extended Binary Coded Decimal Interchange Code

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.

📋 Quick Reference

Code / TopicKey Rule or Property
BCD (8421)4 bits per decimal digit; valid codes: 0000–1001; illegal: 1010–1111
Gray codeNon-weighted; adjacent values differ by exactly 1 bit; used in encoders/ADCs
Excess-3BCD + 0011; self-complementing (9’s complement = bit inversion)
BCD additionIf digit sum > 9 or carry out → add 0110 to that digit
Excess-3 additionCarry: add 0011; No carry: subtract 0011
Even parity bitChosen so total 1-count in codeword is even; detects single-bit errors
Hamming codeParity bits at positions 1,2,4,8,…; syndrome = XOR checks; identifies error position
Hamming p bits needed2ᵖ ≥ m + p + 1 where m = data bits
CRCAppend (n−1) zeros; divide by G(x) mod-2; remainder = CRC; receiver divides frame → remainder should be 0
ASCII7-bit; 128 chars; digits at 0110000; A at 1000001; a at 1100001
EBCDIC8-bit IBM code; 256 chars; letters not contiguous; legacy mainframes
Binary→GrayMSB same; each Gray bit = XOR of current and previous binary bit
Gray→BinaryMSB same; each binary bit = XOR of previous binary bit and current Gray bit
Coming next — DE-03: Boolean Algebra and Logic Gates — postulates and theorems, De Morgan’s theorem, SOP and POS canonical forms, Venn diagrams, and realization using NAND/NOR universal gates.
Scroll to Top