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.
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:
- Mod-2 arithmetic = XOR. Addition and subtraction are the same, with no carry and no borrow.
- In CRC, append as many zeros to the data as the degree. If the generator has 4 bits, append 3 zeros.
- In the Internet checksum, the carry wraps around to the start (wraparound), and the bits are inverted at the end.
- The Hamming syndrome gives the position of the bad bit in binary.
Detection or correction?
The sender adds (error detection and correction) bits to the data bits. The receiver gets and and checks consistency. No method gives a 100% guarantee; the goal is to make the probability of an undetected error very small.
| Method | Extra bits | What it does | Where |
|---|---|---|---|
| Single parity | 1 | Detects an odd number of errors | Simple serial lines |
| 2D parity | rows + columns + 1 | Corrects a single error | Teaching example |
| Internet checksum | 16 | Detection (weak) | IP, TCP, UDP headers |
| CRC | (16, 32) | Strong detection | Ethernet, Wi-Fi |
| Hamming | Corrects a single error | Memory (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 of a code is the smallest distance between two valid codewords.
A code with detects 2 errors or corrects 1 error. Intuition: draw a sphere of radius 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.
- Add the words. If a carry goes beyond 16 bits, add it to the bottom of the result (wraparound, one’s complement addition).
- Invert all bits of the sum. The result is the checksum.
- 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 of bits. The degree of the generator is .
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:
- Append zeros to the end of the data ().
- Divide this by with mod-2 division. At each step, align under the leading 1 and take the XOR. If the leading bit is 0, shift one to the right.
- The last bits are the remainder . Transmitted: followed by .
- The receiver divides the received string (without appending zeros) by . If the remainder is 0, accept; otherwise there is an error.
Worked example: = 11010110, = 1011
, 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 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 catches all burst errors of length less than or equal to . Ethernet’s CRC-32 catches all bursts of 32 bits and below; longer ones with probability .
- Single-bit errors are always caught if has at least two terms.
- A generator containing the factor 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.
| Parity | Positions it checks | Why |
|---|---|---|
| p1 | 1, 3, 5, 7 | lowest bit is 1 in binary |
| p2 | 2, 3, 6, 7 | middle bit is 1 |
| p4 | 4, 5, 6, 7 | top 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 data bits and parity bits there are positions in total; the syndrome must distinguish each of them and also the “no error” case:
, , , .
Common mistakes
| Wrong idea | Correct |
|---|---|
| In CRC, appending as many zeros as the generator length | As many as the degree: 3 zeros for a 4-bit generator. |
= 1011 | Missing degrees are 0: 10011. |
| Borrowing in mod-2 division | No borrow, only XOR. |
| The receiver also appends zeros and divides | The receiver divides the whole received word as it is. |
| Dropping the overflow bit in the checksum | The carry is added at the bottom (wraparound). |
| Checksum = sum | It is the inverse of the sum (one’s complement). |
| Single parity catches double errors | An 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 modification | No, anyone can recompute the CRC. |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
Using even parity, what parity bit is appended to the 7-bit data 1100101?
Show answer
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.
What is the Hamming distance between 10110110 and 10011100?
Show answer
Take the XOR and count the 1s: 10110110 XOR 10011100 = 00101010. There are three 1s, so the distance is 3.
A code has minimum Hamming distance 5. How many bit errors can it detect, and how many can it correct?
- Detect 5, correct 2
- Detect 4, correct 2
- Detect 4, correct 4
- Detect 2, correct 4
Show answer
To detect errors you need , and to correct errors you need .
With : detection , correction .
Compute the CRC remainder for data 1010011 with generator 1101. What bits are transmitted?
Show answer
The generator has 4 bits, so the degree is . 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.
A receiver using generator 1011 receives 1001110101. Is an error detected?
- No, the remainder is 000
- Yes, the remainder is 101
- Yes, the remainder is 011
- Cannot be determined without the original data
Show answer
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.
Compute the 16-bit Internet checksum of the two words 0xA1B2 and 0x7C3D.
Show answer
Add: . There is a carry beyond 16 bits.
Wraparound: add the carry bit back at the bottom: .
One's complement (flip the bits): .
The receiver adds everything including the checksum; if there is no error, the result is 0xFFFF.
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
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.
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
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 : bit 2 is in error. Correct it: 1010101. Data (bits 3,5,6,7): 1101.
How many check bits does a single-error-correcting Hamming code need for data bits?
Show answer
Condition: .: ? No. : ? Yes. The answer is 5 (21 bits in total).
Which error pattern is NOT detected by two-dimensional even parity?
- Any single-bit error
- Two errors in the same row
- Three errors in different rows and columns
- Four errors at the corners of a rectangle
Show answer
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).
Compute the CRC for data 1101101 using the generator polynomial .
Show answer
gives the coefficients of = 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.
Why is CRC preferred over a simple checksum at the link layer?
- CRC can correct any number of errors
- CRC detects all burst errors of length up to r+1 bits and is easy to compute in hardware
- CRC encrypts the frame
- CRC uses fewer bits than a single parity bit
Show answer
With a generator of degree , CRC catches all burst errors of 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).