CS Atlas GATE 2027
Computer Networks course
0 / 90 complete

Computer Networks/Phase 2 · Data Link Layer

Lesson 2.2.2 · Item 27 of 90

Hamming distance: detect d − 1, correct ⌊(d − 1)/2⌋

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. Which errors does a single parity bit detect?
    Stuck? Peek at 2.2.1 Single-bit vs burst errors; redundancy; 1-D and 2-D parity
    • Single-bit vs burst; burst length = first to last damaged bit inclusive.
    • Codeword = dataword (k) + r check bits.
    • 1-D parity: detects odd numbers of flips only.
    • 2-D parity: R + C + 1 check bits; detects all 1–3-bit errors, corrects 1, misses some 4-bit rectangles.

    Read lesson 2.2.1

  2. How many frames carry a packet over 3 links?
    Stuck? Peek at 2.1 Role of the data link layer: hop-to-hop delivery and MAC addresses
    • Data link layer = hop-to-hop delivery of frames to the next directly connected node.
    • Jobs: framing, link addressing, error detection, medium access (+ error/flow control).
    • MAC address: 48 bits, 6 bytes, 12 hex digits; 24-bit vendor prefix; flat.
    • A new frame (new MAC addresses) is built on every link; the packet inside is unchanged.
    • Foundation: bit stuffing inserts a 0 after five consecutive 1s.

    Read lesson 2.1

  3. How do you count positions where two bit strings differ?
    Stuck? Peek at 0.2.3 XOR, 1's complement addition and modulo-2 division
    • AND (mask), OR, NOT (flip), XOR (1 where bits differ).
    • a ⊕ a = 0, a ⊕ 0 = a, (a ⊕ k) ⊕ k = a.
    • 1's complement = flip all bits; 1's-complement addition wraps the end carry back in.
    • Modulo-2: + = − = XOR, no carries; bits ↔ polynomial.
    • Mod-2 division: leading 1 → XOR divisor; leading 0 → XOR zeros; remainder = (divisor length − 1) bits.

    Read lesson 0.2.3

The Hook

Imagine valid words placed on a map so that any two are at least three steps apart. If one letter gets smudged, the damaged word is still closer to the original than to any other valid word, so you can even guess back the original. The distance between valid codewords is what decides how many errors a code can catch or fix.

Predict First

A code has only two codewords: 000 and 111. How many flipped bits can it always detect? How many can it correct?

,

Distance 3. It detects up to 2 flips (no 1- or 2-flip pattern turns 000 into 111) and corrects 1 (one flip leaves the word closer to its original — majority vote).

Core Concept

Hamming distance

Hamming distance d(x, y) = the number of positions in which two equal-length bit strings differ = the number of 1s in x ⊕ y.
Minimum distance dmin of a code = the smallest Hamming distance between any two distinct codewords.
01001differdiffer
10101 ⊕ 11100 = 01001 → d = 2.

Detection and correction rules

  • To detect up to s errors: dmin ≥ s + 1 → a code detects dmin − 1 errors.
  • To correct up to t errors: dmin ≥ 2t + 1 → a code corrects ⌊(dmin − 1)/2⌋ errors.

Why: s flips must not reach another codeword; for correction, the spheres of radius t around codewords must not overlap.

d_mindetectscorrects
100
210
321
431
542
763
The two rules as a lookup table.

Hamming code size (how many check bits)

A single-error-correcting Hamming code for m data bits needs the smallest r with 2ʳ ≥ m + r + 1: the r check bits must name every one of the m + r positions, plus "no error".

data bits mcheck bits rcodeword m + r
437
7411
11415
16521
32638
Values of r from 2ʳ ≥ m + r + 1 (computed).

Putting it together

  • Hamming distance counts differing bits; d_min is the code's weakest pair.
  • Detect d − 1 errors, correct ⌊(d − 1)/2⌋.
  • Hamming codes need r with 2ʳ ≥ m + r + 1.

Visual

111111110111000001011010
Codewords 000 and 111 are 3 flips apart. One flip lands nearer the original (correctable); two flips still are not a codeword (detectable).

Worked Examples

Easy

Hamming distance between 10101 and 11100?

Show the full solution

XOR = 01001 → 2.

Medium

Code {00000, 01011, 10101, 11110}. Find dmin, and how many errors it detects and corrects.

Show the full solution

Pairwise distances: 3, 3, 4, 4, 3, 3 → dmin = 3. Detects 3 − 1 = 2; corrects ⌊2/2⌋ = 1.

GATE

(a) What minimum distance is needed to detect 4 errors? To correct 4 errors? (b) How many check bits does a single-error-correcting Hamming code need for 7 and for 16 data bits?

Show the full solution

(a) Detect 4: d ≥ 5. Correct 4: d ≥ 2 × 4 + 1 = 9.

(b) m = 7: 2⁴ = 16 ≥ 7 + 4 + 1 = 12, while 2³ = 8 < 11 → r = 4. m = 16: 2⁵ = 32 ≥ 22, 2⁴ = 16 < 21 → r = 5.

GATE Traps

  • dmin is the minimum over all pairs, not the average or maximum.
  • Correction count uses the floor: d = 4 corrects only 1.
  • A code cannot simultaneously correct t and detect s unless d ≥ t + s + 1; questions usually ask one or the other.

Memory Hook

"Detect = one less than the gap; correct = less than half the gap."

Connect the Dots

Distance tells us the strength of any code. Next (2.2.3): a very cheap detection code used inside headers on the Internet — adding the data in 1's complement (Lesson 0.2.3) and sending the complement of the sum.

Self-Test

  1. NAT

    Hamming distance between 1100110 and 1010101?

    Write your answer before opening the key.

  2. NAT

    A code has d_min = 6. How many errors can it correct?

    Write your answer before opening the key.

  3. MCQ

    To detect up to 3 errors, the minimum distance must be at least:

    1. 3
    2. 4
    3. 6
    4. 7
  4. NAT

    Check bits in a single-error-correcting Hamming code for 11 data bits?

    Write your answer before opening the key.

  5. MSQ

    For the code {000000, 010111, 101011, 111100}:

    1. d_min = 4
    2. it detects 3 errors
    3. it corrects 2 errors
    4. it corrects 1 error

Rule Card

  • d(x, y) = number of 1s in x ⊕ y; d_min = smallest over codeword pairs.
  • Detect d_min − 1; correct ⌊(d_min − 1)/2⌋.
  • Detect s needs d ≥ s + 1; correct t needs d ≥ 2t + 1.
  • Hamming code: smallest r with 2ʳ ≥ m + r + 1.

Concept Ledger

  • Hamming distance
  • minimum distance d_min
  • detect d − 1, correct ⌊(d − 1)/2⌋
  • Hamming code check bits 2ʳ ≥ m + r + 1

Answer Vault

Try the questions first. Open when you are ready to check.

Warm-up answers

  1. Any odd number of flipped bits.
  2. 3.
  3. Count the 1s in their XOR.

Self-test answers

  1. 4 — XOR = 0110011 → four 1s.
  2. 2 — ⌊(6 − 1)/2⌋ = 2.
  3. B — d ≥ s + 1 = 4.
    • A: 3 detects only 2.
    • C: That is for correction-type bounds incorrectly applied.
    • D: That corrects 3.
  4. 4 — 2⁴ = 16 ≥ 11 + 4 + 1 = 16 → r = 4.
  5. ABD — d_min = 4 → detects 3, corrects 1.
    • C: Correcting 2 needs d ≥ 5.

Flashcards

  1. Errors detected with d_min?
    Reveal answerd_min − 1.
  2. Errors corrected with d_min?
    Reveal answer⌊(d_min − 1)/2⌋.
  3. Hamming check bits for m data bits?
    Reveal answerSmallest r with 2ʳ ≥ m + r + 1.

Exam shortcuts

  • Detect s ⇔ d ≥ s + 1; correct t ⇔ d ≥ 2t + 1. — any block code; detect and correct counts are separate guarantees, not simultaneous.
  • Hamming check bits: smallest r with 2ʳ ≥ m + r + 1. — single-error-correcting Hamming codes.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.