Data Link Layer and Medium Access Sub Layer · Computer Networks

Hamming Distance

Watch lectures

Hamming Distance and Hamming Codes


1. Definition of Hamming Distance

The Hamming Distance between two words (binary sequences of equal length) xx and yy, denoted as d(x,y)d(x, y), is the number of bit positions in which the corresponding bits differ.

It is mathematically calculated by performing a bitwise Exclusive-OR (XOR) operation between the two words and counting the number of 1s in the result (the Hamming Weight ww):

d(x,y)=w(x⊕y)d(x, y) = w(x \oplus y)
SVGVector Architecture Diagram (SVG)
Bit Position 1 2 3 4 5 6 x: 1 0 1 1 0 1 y: 1 0 0 1 1 1 x ⊕ y: 0 0 1 0 1 0 ===> Count of 1s = 2 Hamming Distance d(x, y) = 2

Minimum Hamming Distance (dmin⁡d_{\min}):

The Minimum Hamming Distance dmin⁡d_{\min} of a code is the smallest Hamming distance between any pair of distinct valid codewords in the entire codebook:

dmin⁡=min⁡{d(Ci,Cj)∣CieqCj}d_{\min} = \min \{ d(C_i, C_j) \mid C_i eq C_j \}

(For linear block codes, dmin⁡d_{\min} equals the minimum weight among all non-zero valid codewords)).


2. Detection and Correction Capabilities

The error detection and correction capabilities of any block code are strictly bounded by its minimum Hamming distance dmin⁡d_{\min}:

MermaidDiagram
Rendering diagram…
MermaidDiagram
Rendering diagram…

A. Error Detection Condition

To detect up to ss bit errors, the minimum Hamming distance must satisfy:

dmin⁡≥s+1d_{\min} \geq s + 1
  • Example: To detect all single-bit errors (s=1s = 1), we need dmin⁡≥1+1=2d_{\min} \geq 1 + 1 = 2 (achieved by simple parity check).
  • To detect all 2-bit errors (s=2s = 2), we need dmin⁡≥2+1=3d_{\min} \geq 2 + 1 = 3.

B. Error Correction Condition

To correct up to tt bit errors, the minimum Hamming distance must satisfy:

dmin⁡≥2t+1d_{\min} \geq 2t + 1
  • Example: To correct any single-bit error (t=1t = 1):
dmin⁡≥2(1)+1=3d_{\min} \geq 2(1) + 1 = 3
  • If a single bit flips, the received word is at distance 1 from the original valid codeword and at least distance 2 from any other valid codeword, enabling unambiguous nearest-neighbor decoding.

3. The Hamming Code (Hamming Rule & Construction)

Richard Hamming designed an elegant single-error-correcting linear code.

Redundancy Bit Formula:

If an information message contains mm data bits and requires rr redundant parity bits, the total codeword length is n=m+rn = m + r. The rr parity bits must distinguish between nn single-bit error locations and 1 error-free state (n+1n + 1 total states):

2r≥m+r+12^r \geq m + r + 1
Data Bits (mm) Redundant Bits (rr) Total Codeword Length (n=m+rn = m + r) Example Code
1 2 3 (3, 1) Triple Repetition
4 3 7 (7, 4) Hamming Code
11 4 15 (15, 11) Hamming Code
26 5 31 (31, 26) Hamming Code

4. Construction of the Hamming (7, 4) Code

A (7, 4) Hamming code carries m=4m = 4 data bits (D7,D6,D5,D3D_7, D_6, D_5, D_3) and r=3r = 3 parity bits (P4,P2,P1P_4, P_2, P_1).

Bit Position Mapping:

Parity bits are placed strictly in bit positions that are powers of 2 (1,2,4,8,…1, 2, 4, 8, \dots). All other positions hold data bits:

Bit Position 7 6 5 4 3 2 1
Binary Index 111 110 101 100 011 010 001
Role D7D_7 D6D_6 D5D_5 P4P_4 D3D_3 P2P_2 P1P_1

Parity Check Bit Calculation (Even Parity):

Each parity bit checks all bit positions whose binary representation has a 1 in the parity bit's position:

  • P1P_1 (checks positions with LSB 1: 1, 3, 5, 7):
P1=D3⊕D5⊕D7P_1 = D_3 \oplus D_5 \oplus D_7
  • P2P_2 (checks positions with 2nd bit 1: 2, 3, 6, 7):
P2=D3⊕D6⊕D7P_2 = D_3 \oplus D_6 \oplus D_7
  • P4P_4 (checks positions with 3rd bit 1: 4, 5, 6, 7):
P4=D5⊕D6⊕D7P_4 = D_5 \oplus D_6 \oplus D_7

5. Solved Step-by-Step Example

Step 1: Encoding at Sender

Let the 4-bit dataword be 1 0 1 1 (D7=1,D6=0,D5=1,D3=1D_7 = 1, D_6 = 0, D_5 = 1, D_3 = 1).

  • P1=D3⊕D5⊕D7=1⊕1⊕1=1P_1 = D_3 \oplus D_5 \oplus D_7 = 1 \oplus 1 \oplus 1 = 1
  • P2=D3⊕D6⊕D7=1⊕0⊕1=0P_2 = D_3 \oplus D_6 \oplus D_7 = 1 \oplus 0 \oplus 1 = 0
  • P4=D5⊕D6⊕D7=1⊕0⊕1=0P_4 = D_5 \oplus D_6 \oplus D_7 = 1 \oplus 0 \oplus 1 = 0
Transmitted 7-bit Codeword [b7b6b5b4b3b2b1]=1 0 1 0 1 0 1\text{Transmitted 7-bit Codeword } [b_7 b_6 b_5 b_4 b_3 b_2 b_1] = \mathbf{1\ 0\ 1\ 0\ 1\ 0\ 1}

Step 2: Error Injection during Transmission

Suppose bit 5 flips during transit (b5b_5 changes from 1 to 0):

Received Word =1 0 0 0 1 0 1\text{Received Word } = \mathbf{1\ 0\ 0\ 0\ 1\ 0\ 1}

Step 3: Syndrome Calculation at Receiver

Compute syndrome bits (s4,s2,s1)(s_4, s_2, s_1) using even parity check:

  • s1=b1⊕b3⊕b5⊕b7=1⊕1⊕0⊕1=1s_1 = b_1 \oplus b_3 \oplus b_5 \oplus b_7 = 1 \oplus 1 \oplus 0 \oplus 1 = \mathbf{1}
  • s2=b2⊕b3⊕b6⊕b7=0⊕1⊕0⊕1=0s_2 = b_2 \oplus b_3 \oplus b_6 \oplus b_7 = 0 \oplus 1 \oplus 0 \oplus 1 = \mathbf{0}
  • s4=b4⊕b5⊕b6⊕b7=0⊕0⊕0⊕1=1s_4 = b_4 \oplus b_5 \oplus b_6 \oplus b_7 = 0 \oplus 0 \oplus 0 \oplus 1 = \mathbf{1}
Syndrome Vector S=[s4 s2 s1]=[1 0 1]2=510\text{Syndrome Vector } S = [s_4\ s_2\ s_1] = [1\ 0\ 1]_2 = \mathbf{5}_{10}

The syndrome decimal value 55 identifies Bit 5 as the erroneous bit! The receiver flips bit 5 back from 0 to 1, completely correcting the error.


Frequently Asked Questions

QState the relationship between $d_{\min}$ and error detection/correction.
  • Detection: dmin⁡≥s+1d_{\min} \geq s + 1.
  • Correction: dmin⁡≥2t+1d_{\min} \geq 2t + 1.
QIf a code has $d_{\min} = 5$, what are its maximum error detection and correction capabilities?
  • Max detection: s=dmin⁡−1=5−1=4s = d_{\min} - 1 = 5 - 1 = 4 bit errors.
  • Max correction: t=⌊dmin⁡−12⌋=⌊42⌋=2t = \lfloor \frac{d_{\min} - 1}{2} \rfloor = \lfloor \frac{4}{2} \rfloor = 2 bit errors.
QWhat is the Hamming inequality for calculating parity bits?

2r≥m+r+12^r \geq m + r + 1, where mm is the number of data bits and rr is the number of parity bits.

QHow does the syndrome locate the error in a Hamming code?

When parity check equations are evaluated, the resulting syndrome binary bits [sk…s1]2[s_k \dots s_1]_2 directly form the 1-based decimal index of the corrupted bit. If syndrome is 0, no error occurred.

QWhere are the parity bits placed in a Hamming code?

At bit positions that are powers of 2 (1, 2, 4, 8, ...). All other positions hold data bits, and each parity bit checks the positions whose binary index has a 1 in that parity bit's place.

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.

← All Computer Networks modules