SEN2008 · Week 3 · Data Link Layer Concepts (I) : error detection and correction, elementary protocols, sliding window protocols

Error detection and correction: parity, checksum, CRC, Hamming

How the link layer catches bit errors: one- and two-dimensional parity, the Internet checksum, CRC polynomial division, and single-bit correction with a Hamming code. On the exam, all of these come as hand-calculation questions.

error detectionerror correctionparity bittwo-dimensional parityInternet checksumone's complementCRC (cyclic redundancy check)generator polynomialmodulo-2 arithmeticHamming codeHamming distancesyndromeburst errorFEC (forward error correction)

The physical layer is not perfect. Noise, interference and weak signals flip some bits. The link layer’s first job is to detect these errors (detection), and sometimes to correct them (correction). On the exam this topic is almost always a hand-calculation question: “compute the CRC”, “compute the checksum”, “which bit is in error?”. You will memorize procedures, not formulas.

Golden rules:

  1. Mod-2 arithmetic = XOR. Addition and subtraction are the same, with no carry and no borrow.
  2. In CRC, append as many zeros to the data as the degree. If the generator has 4 bits, append 3 zeros.
  3. In the Internet checksum, the carry wraps around to the start (wraparound), and the bits are inverted at the end.
  4. The Hamming syndrome gives the position of the bad bit in binary.

Detection or correction?

The sender adds EDCEDC (error detection and correction) bits to the DD data bits. The receiver gets D′D' and EDC′EDC' and checks consistency. No method gives a 100% guarantee; the goal is to make the probability of an undetected error very small.

MethodExtra bitsWhat it doesWhere
Single parity1Detects an odd number of errorsSimple serial lines
2D parityrows + columns + 1Corrects a single errorTeaching example
Internet checksum16Detection (weak)IP, TCP, UDP headers
CRCrr (16, 32)Strong detectionEthernet, Wi-Fi
Hamming≈log⁡2m\approx \log_2 mCorrects a single errorMemory (ECC RAM)

Error correction (FEC, forward error correction) lets the receiver correct errors without asking for retransmission. It is valuable on links with large delay, such as satellites. Error detection + retransmission (ARQ) is cheaper on short-delay links. You will see retransmission on the second page of this week.

Parity

One-dimensional parity. One bit is appended to the data. With even parity the total number of 1s is even, with odd parity it is odd.

Data 1011001  -> 1s: 4 (even)
even parity bit = 0  -> 10110010
odd  parity bit = 1  -> 10110011

If a single bit flips, the parity breaks and the error is caught. If two bits flip, the parity still holds: an even number of errors escapes. Since errors usually come in bursts, this is a serious weakness.

Two-dimensional parity. The data is written in a table, and parity is computed for every row and every column:

          c0 c1 c2 c3 | row parity
row 1:     1  0  1  1 |  1
row 2:     0  1  1  0 |  0
row 3:     1  1  0  0 |  0
-----------------------+---
column:    0  0  0  1 |  1   (corner)

If the bit in row 2, column 2 flips (0110 → 0100), the parity of row 2 and the parity of column 2 break. The intersection points to a single bit: the bad bit is found and corrected.

PITFALL · the rectangle error. If the bits at the four corners of a rectangle flip together, every row and every column sees two changes, and all parities look correct. 2D parity misses this.

Hamming distance

The Hamming distance of two bit strings is the number of bits in which they differ (the number of 1s in the XOR). The minimum distance dmind_{min} of a code is the smallest distance between two valid codewords.

detection: dmin≥s+1correction: dmin≥2t+1\text{detection: } d_{min} \ge s + 1 \qquad \text{correction: } d_{min} \ge 2t + 1

A code with dmin=3d_{min} = 3 detects 2 errors or corrects 1 error. Intuition: draw a sphere of radius tt around each valid word; if the spheres do not overlap, you can correct by “rounding” to the nearest valid word.

Internet checksum

Used in IP, UDP and TCP headers. The data is split into 16-bit words.

  1. Add the words. If a carry goes beyond 16 bits, add it to the bottom of the result (wraparound, one’s complement addition).
  2. Invert all bits of the sum. The result is the checksum.
  3. The receiver adds all the words including the checksum. If there is no error, the result is 0xFFFF (all bits 1).

Worked example

Words: 0x6E2B, 0x9C71, 0x13A5.

  6E2B
+ 9C71
------
 10A9C      bit 17 overflowed (carry = 1)
  0A9C + 1 = 0A9D        wraparound
+ 13A5
------
  1E42      no overflow
checksum = ~1E42 = FFFF - 1E42 = E1BD

Receiver: 6E2B + 9C71 + 13A5 + E1BD  ->  FFFF  (no error)

Computing the checksum is cheap in software but weak: for example, it does not notice at all if two words are swapped. That is why the stronger CRC is used at the link layer.

CRC (cyclic redundancy check)

In CRC, bit strings are treated as the coefficients of polynomials. The sender and receiver agree on a generator GG of r+1r+1 bits. The degree of the generator is rr.

From polynomial to bit string: one bit for each degree, 0 for a missing degree.

x^3 + x + 1      = 1·x^3 + 0·x^2 + 1·x + 1  -> 1011
x^4 + x + 1      -> 10011
x^4 + x^3 + 1    -> 11001

Procedure:

  1. Append rr zeros to the end of the data DD (D⋅2rD \cdot 2^r).
  2. Divide this by GG with mod-2 division. At each step, align GG under the leading 1 and take the XOR. If the leading bit is 0, shift one to the right.
  3. The last rr bits are the remainder RR. Transmitted: DD followed by RR.
  4. The receiver divides the received string DRD R (without appending zeros) by GG. If the remainder is 0, accept; otherwise there is an error.

Worked example: DD = 11010110, GG = 1011

r=3r = 3, append three zeros. On each line we write the dividend as it looks after the XOR:

11010110000        D + 000
1011               align with the leading 1, XOR
01100110000
 1011
00111110000
  1011
00010010000
   1011
00000100000        skip the leading zeros
     1011
00000001100
       1011
00000000111        remainder R = 111 (last 3 bits)

Transmitted: 11010110 111
Check      : 11010110111 / 1011  -> remainder 000  (correct)

TIP · quick check. The remainder is always rr bits (one bit shorter than the generator). If your remainder came out as 4 bits, you misaligned something.

Why is CRC strong?

  • A generator of degree rr catches all burst errors of length less than or equal to rr. Ethernet’s CRC-32 catches all bursts of 32 bits and below; longer ones with probability 1−0.5r1 - 0.5^{r}.
  • Single-bit errors are always caught if GG has at least two terms.
  • A generator containing the factor (x+1)(x+1) catches all odd numbers of errors.
  • In hardware it runs at line rate with a shift register and a few XOR gates.

PITFALL · CRC is not a security mechanism. CRC was designed against random errors. A malicious party can modify the frame and recompute the CRC, because the algorithm is not secret. Against deliberate modification you need a keyed MAC (week 12).

Hamming code: single-bit correction

Hamming(7,4) adds 3 parity bits to 4 data bits. The parity bits sit at positions that are powers of 2 (1, 2, 4), and the data bits at the rest (3, 5, 6, 7). Each parity bit checks the positions whose binary representation contains its own bit.

ParityPositions it checksWhy
p11, 3, 5, 7lowest bit is 1 in binary
p22, 3, 6, 7middle bit is 1
p44, 5, 6, 7top bit is 1

Encoding: data 1011

d3=1, d5=0, d6=1, d7=1
p1 = d3 ⊕ d5 ⊕ d7 = 1 ⊕ 0 ⊕ 1 = 0
p2 = d3 ⊕ d6 ⊕ d7 = 1 ⊕ 1 ⊕ 1 = 1
p4 = d5 ⊕ d6 ⊕ d7 = 0 ⊕ 1 ⊕ 1 = 0
position : 1  2  3  4  5  6  7
bit      : 0  1  1  0  0  1  1     -> 0110011

Decoding: bit 6 flipped, 0110001 received

s1 = b1⊕b3⊕b5⊕b7 = 0⊕1⊕0⊕1 = 0
s2 = b2⊕b3⊕b6⊕b7 = 1⊕1⊕0⊕1 = 1
s4 = b4⊕b5⊕b6⊕b7 = 0⊕0⊕0⊕1 = 1
syndrome s4 s2 s1 = 110 (binary) = 6   -> flip bit 6 -> 0110011

If the syndrome is 0, there is no error. This code cannot correctly fix two errors; it “corrects” a wrong bit.

How many parity bits are needed? With mm data bits and rr parity bits there are m+rm + r positions in total; the syndrome must distinguish each of them and also the “no error” case:

2r≥m+r+12^r \ge m + r + 1

m=4⇒r=3m = 4 \Rightarrow r = 3, m=8⇒r=4m = 8 \Rightarrow r = 4, m=16⇒r=5m = 16 \Rightarrow r = 5, m=32⇒r=6m = 32 \Rightarrow r = 6.

Common mistakes

Wrong ideaCorrect
In CRC, appending as many zeros as the generator lengthAs many as the degree: 3 zeros for a 4-bit generator.
x4+x+1x^4 + x + 1 = 1011Missing degrees are 0: 10011.
Borrowing in mod-2 divisionNo borrow, only XOR.
The receiver also appends zeros and dividesThe receiver divides the whole received word as it is.
Dropping the overflow bit in the checksumThe carry is added at the bottom (wraparound).
Checksum = sumIt is the inverse of the sum (one’s complement).
Single parity catches double errorsAn even number of errors does not change the parity.
The Hamming syndrome says “which parity is broken”The binary value of the syndrome is directly the position of the bad bit.
CRC prevents deliberate modificationNo, anyone can recompute the CRC.

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

Using even parity, what parity bit is appended to the 7-bit data 1100101?

Show answer
0

Even parity: the total number of 1s must be even. In 1100101 the 1s are: 1,1,1,1, so there are 4. That is already even, so the parity bit is 0.

Question 2easy

What is the Hamming distance between 10110110 and 10011100?

Show answer
3

Take the XOR and count the 1s: 10110110 XOR 10011100 = 00101010. There are three 1s, so the distance is 3.

Question 3medium

A code has minimum Hamming distance 5. How many bit errors can it detect, and how many can it correct?

  1. Detect 5, correct 2
  2. Detect 4, correct 2
  3. Detect 4, correct 4
  4. Detect 2, correct 4
Show answer
B) Detect 4, correct 2

To detect dd errors you need dmin≥d+1d_{min} \ge d+1, and to correct tt errors you need dmin≥2t+1d_{min} \ge 2t+1.
With dmin=5d_{min} = 5: detection 5−1=45 - 1 = 4, correction ⌊(5−1)/2⌋=2\lfloor (5-1)/2 \rfloor = 2.

Question 4medium

Compute the CRC remainder for data 1010011 with generator 1101. What bits are transmitted?

Show answer
R = 111, transmitted 1010011111

The generator has 4 bits, so the degree is r=3r = 3. Append 3 zeros to the data: 1010011000.
Mod-2 division (XOR, no borrow). At each step, place 1101 under the leading 1:1010 XOR 1101 = 0111; then 1110 XOR 1101 = 0011; 1111 XOR 1101 = 0010; 1000 XOR 1101 = 0101; 1010 XOR 1101 = 0111.
The last 3 bits are the remainder: R = 111. Transmitted: 1010011 + 111 = 1010011111.

Question 5medium

A receiver using generator 1011 receives 1001110101. Is an error detected?

  1. No, the remainder is 000
  2. Yes, the remainder is 101
  3. Yes, the remainder is 011
  4. Cannot be determined without the original data
Show answer
B) Yes, the remainder is 101

The receiver divides the whole received word (without appending zeros) by the generator. If the remainder is not 0, there is an error.
At the end of the mod-2 division the remainder is 101, which is not zero. The error is detected and the frame is discarded.

Question 6medium

Compute the 16-bit Internet checksum of the two words 0xA1B2 and 0x7C3D.

Show answer
0xE20F

Add: A1B2+7C3D=11DEF\mathtt{A1B2} + \mathtt{7C3D} = \mathtt{11DEF}. There is a carry beyond 16 bits.
Wraparound: add the carry bit back at the bottom: 1DEF+1=1DF0\mathtt{1DEF} + 1 = \mathtt{1DF0}.
One's complement (flip the bits): FFFF−1DF0=E20F\mathtt{FFFF} - \mathtt{1DF0} = \mathtt{E20F}.
The receiver adds everything including the checksum; if there is no error, the result is 0xFFFF.

Question 7medium

Encode the 4 data bits 0110 (d3 d5 d6 d7) with a Hamming(7,4) code using even parity, bit positions 1..7 = p1 p2 d3 p4 d5 d6 d7.

Show answer
1100110

d3 = 0, d5 = 1, d6 = 1, d7 = 0.
p1 = d3 ⊕ d5 ⊕ d7 = 0 ⊕ 1 ⊕ 0 = 1.
p2 = d3 ⊕ d6 ⊕ d7 = 0 ⊕ 1 ⊕ 0 = 1.
p4 = d5 ⊕ d6 ⊕ d7 = 1 ⊕ 1 ⊕ 0 = 0.
In the order p1 p2 d3 p4 d5 d6 d7: 1 1 0 0 1 1 0 = 1100110.

Question 8hard

A Hamming(7,4) codeword (same layout, even parity) is received as 1110101. Which bit is in error, and what are the corrected data bits d3 d5 d6 d7?

Show answer
bit 2; data 1101

Bits: b1=1 b2=1 b3=1 b4=0 b5=1 b6=0 b7=1.
s1 = b1⊕b3⊕b5⊕b7 = 1⊕1⊕1⊕1 = 0.
s2 = b2⊕b3⊕b6⊕b7 = 1⊕1⊕0⊕1 = 1.
s4 = b4⊕b5⊕b6⊕b7 = 0⊕1⊕0⊕1 = 0.
Syndrome s4s2s1=0102=2s_4 s_2 s_1 = 010_2 = 2: bit 2 is in error. Correct it: 1010101. Data (bits 3,5,6,7): 1101.

Question 9medium

How many check bits rr does a single-error-correcting Hamming code need for m=16m = 16 data bits?

Show answer
5

Condition: 2r≥m+r+12^r \ge m + r + 1.r=4r = 4: 16≥2116 \ge 21? No. r=5r = 5: 32≥2232 \ge 22? Yes. The answer is 5 (21 bits in total).

Question 10hard

Which error pattern is NOT detected by two-dimensional even parity?

  1. Any single-bit error
  2. Two errors in the same row
  3. Three errors in different rows and columns
  4. Four errors at the corners of a rectangle
Show answer
D) Four errors at the corners of a rectangle

Four errors at the corners of a rectangle change 2 bits in each affected row and 2 bits in each affected column. All parities stay even, so the error is invisible.
Two errors in the same row do not break the row parity but break two column parities, so they are detected (but cannot be corrected).

Question 11hard

Compute the CRC for data 1101101 using the generator polynomial x4+x+1x^4 + x + 1.

Show answer
1001

x4+x+1x^4 + x + 1 gives the coefficients of x4,x3,x2,x1,x0x^4, x^3, x^2, x^1, x^0 = 1 0 0 1 1. The degree is 4, so append 4 zeros to the data: 11011010000.
Steps: 11011⊕10011=01000; 10000⊕10011=00011; 11100⊕10011=01111; 11110⊕10011=01101; 11010⊕10011=01001.
Remainder (last 4 bits): 1001. Transmitted: 11011011001.

Question 12easy

Why is CRC preferred over a simple checksum at the link layer?

  1. CRC can correct any number of errors
  2. CRC detects all burst errors of length up to r+1 bits and is easy to compute in hardware
  3. CRC encrypts the frame
  4. CRC uses fewer bits than a single parity bit
Show answer
B) CRC detects all burst errors of length up to r+1 bits and is easy to compute in hardware

With a generator of degree rr, CRC catches all burst errors of r+1r+1 bits or shorter, and longer ones with very high probability. It is fast in hardware with a shift-register XOR circuit.
CRC is not encryption and gives no protection against deliberate modification: an attacker can change the data and recompute the CRC. For integrity you need a MAC (week 12).

Verification: All parity values, checksums, CRC remainders, Hamming codewords and syndromes were computed with node (verify/SEN2008/03a.mjs: mod-2 division trace, 16-bit one's complement sum, Hamming(7,4) encoder/decoder, the 2^r >= m+r+1 loop).

Review card

Parity: even -> total number of 1s is even. Detects 1 bit, no correction, misses double errors 2D parity: FINDS and CORRECTS a single-bit error (row x column intersection); 4 rectangle corners escape Hamming distance d: detects d-1 errors, corrects floor((d-1)/2) Checksum: add 16-bit words, add the CARRY back at the bottom (wrap), then invert (~) receiver: sum is FFFF CRC: G has degree r -> append r zeros to the data -> mod-2 divide (XOR) -> last r bits = R send D + R ; receiver divides the whole word, remainder 0 means OK x^4+x+1 = 10011 x^3+x+1 = 1011 (write 0 for a missing degree!) CRC: catches all bursts of r+1 bits and shorter; easy in hardware Hamming(7,4): p1 p2 d3 p4 d5 d6 d7 p1:1,3,5,7 p2:2,3,6,7 p4:4,5,6,7 syndrome s4 s2 s1 = position of the bad bit (0 means no error) number of check bits: 2^r >= m + r + 1 (m=4->3, 8->4, 16->5, 32->6) None of these protects against an attacker: for deliberate changes you need a MAC/hash