Data Communication · Lecture 11
Framing and error control: stuffing, parity, Hamming codes, CRC and checksums
Md Mahbub E Noor · Assistant Professor, Department of CSE, University of Barishal
Course: Data Communication · 2nd Year, 2nd Semester
Lectures 2–10 gave us
a physical layer that moves raw bits, with noise flipping some of them.
Today
package the bits into frames and add redundancy so the receiver can spot, and sometimes fix, errors.
Reading: Forouzan (5th ed.), Chapter 10 and Section 11.1
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
2 / 36
A
R1
R2
B
frame
one link = one data link job
| Sublayer | Job |
|---|---|
| DLC (data link control) | framing, flow and error control, for every link |
| MAC (media access control) | sharing a broadcast link (Lecture 13) |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
3 / 36
No boundaries needed: the size is the delimiter. Example: ATM cells, always 53 bytes.
53 B
53 B
53 B
53 B
53 B
Mark where each frame starts and ends with a flag:
Flag
Header
Data (variable)
Trailer
Flag
| Character-oriented | Bit-oriented | |
|---|---|---|
| Data | 8-bit characters (text) | any bit pattern |
| Flag | a special byte | 01111110 |
| Problem fix | byte stuffing | bit stuffing |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
4 / 36
Data from upper layer (contains FLAG and ESC)
A
FLAG
B
ESC
C
Frame sent (orange = stuffed ESC)
FLAG
A
ESC
FLAG
B
ESC
ESC
C
FLAG
Receiver removes each ESC and keeps the byte after it
A
FLAG
B
ESC
C
Rule: put an ESC before every FLAG or ESC that appears in the data.
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
5 / 36
01111110: six 1s in a rowData from upper layer
0
0
0
1
1
1
1
1
1
1
0
0
1
1
1
1
1
0
1
0
0
0
Frame body sent (orange = stuffed 0)
0
0
0
1
1
1
1
1
0
1
1
0
0
1
1
1
1
1
0
0
1
0
0
0
Inside the frame: 01111110 | stuffed data | 01111110
Receiver rule
after 0 1 1 1 1 1 : next bit 0 → delete it
1 1 1 1 1 0 → destuff
Next bit after five 1s is 1? Then it is the flag (or an error).
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
6 / 36
Interactive
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
7 / 36
Checkpoint 1 · 3 minutes
Byte-stuff the data: A ESC FLAG B (write the frame body).
Bit-stuff the data 0111111011111100. How many 0s are added?
A receiver gets the frame body 0111110110. What was the original data?
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
8 / 36
sent
0
0
0
0
0
0
1
0
received
0
0
0
0
0
0
0
0
sent
0
1
0
0
0
1
0
0
0
1
0
0
0
0
1
1
received
0
1
0
1
1
1
0
1
0
1
1
0
0
0
1
1
Length = from the first to the last corrupted bit (blue span); bits inside may be correct
Why bursts are common
noise lasting 1/100 s at 1 Mbps
10,000 bits hit
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
9 / 36
Is there an error? Yes/no. Then discard and ask again (retransmission, ARQ).
Which bits are wrong? Fix them at the receiver: forward error correction (FEC).
Add and subtract are the same: XOR. No carries.
| ⊕ | 0 | 1 |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 1 | 0 |
Example
10110 ⊕ 11010 ───── 01100
Equal bits → 0, different bits → 1
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
10 / 36
k data bits
r redundant
dataword (k bits) → codeword (n = k + r bits)
Datawords
2k
valid codewords
Possible n-bit words
2n
2n − 2k invalid
If a received word is invalid, an error is detected
Example: 4B/5B (Lecture 6)
k = 4, n = 5: 2⁴ = 16 valid of 2⁵ = 32
16 unused codes
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
11 / 36
| Dataword | Codeword |
|---|---|
00 | 000 |
01 | 011 |
10 | 101 |
11 | 110 |
Receive 011
valid codeword
accept 01 ✓
Receive 111 (one error)
not in the table
discard ✓
Receive 000 (two errors)
sent 011, valid by bad luck
accept 00 ✗
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
12 / 36
Sent 01011 · received
0
1
0
0
1
| Dataword | Codeword | Distance to 01001 |
|---|---|---|
00 | 00000 | 2 |
01 | 01011 | 1 |
10 | 10101 | 3 |
11 | 11110 | 4 |
Closest valid codeword
01011 differs in only one bit
correct to 01011 → data 01
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
13 / 36
000 ⊕ 011 ─── 011 → two 1s
d = 2
10101 ⊕ 11110 ───── 01011 → three 1s
d = 3
| Code | Pairwise distances | dmin |
|---|---|---|
| 2→3 (Ex. 10.1) | 2, 2, 2, 2, 2, 2 | 2 |
| 2→5 (last slide) | 3, 3, 4, 4, 3, 3 | 3 |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
14 / 36
Detect up to s errors
need
dmin = s + 1
Correct up to t errors
need
dmin = 2t + 1
dmin
received word
Each codeword owns a circle of radius t; if circles do not overlap, the received word belongs to exactly one
dmin 2: detect 1 · dmin 3: detect 2 or correct 1 · dmin 4: detect 3 or correct 1 · dmin 5: detect 4 or correct 2
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
15 / 36
Checkpoint 2 · 3 minutes
Sent 0110100110110001, received 0111100110100001. Burst length?
Find dmin for the codewords 00000, 01101, 10110, 11011.
A code must correct 2 errors. What is the smallest dmin? How many errors would it then detect?
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
16 / 36
r₀ = a₃ ⊕ a₂ ⊕ a₁ ⊕ a₀
s₀ = b₃ ⊕ b₂ ⊕ b₁ ⊕ b₀ ⊕ q₀
Dataword 1011 → codeword 10111 (even parity)
| Case | Received | Syndrome | Result |
|---|---|---|---|
| 1 · no error | 10111 | 0 | accept 1011 ✓ |
| 2 · a1 flipped | 10011 | 1 | discard ✓ |
| 3 · r0 flipped | 10110 | 1 | discard ✓ |
| 4 · r0, a3 flipped | 00110 | 0 | accept 0011 ✗ |
| 5 · a3, a2, a1 flipped | 01011 | 1 | discard ✓ |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
17 / 36
Sent: 4 × 7 data bits
1
1
0
0
1
1
1
1
1
0
1
1
1
0
1
1
0
1
1
1
0
0
1
0
0
1
0
1
0
0
1
1
0
1
0
1
0
1
0
1
blue = row and column parity
Two errors in row 2
1
1
0
0
1
1
1
1
1
0
0
1
1
1
1
1
0
1
1
1
0
0
1
0
0
1
0
1
0
0
1
1
0
1
0
1
0
1
0
1
Row 2 parity still OK, but columns 3 and 6 fail → detected
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
18 / 36
Interactive
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
19 / 36
r₀ = a₂ ⊕ a₁ ⊕ a₀
r₁ = a₃ ⊕ a₂ ⊕ a₁
r₂ = a₁ ⊕ a₀ ⊕ a₃
Worked: dataword 0111
r₀ = 1⊕1⊕1 = 1 · r₁ = 0⊕1⊕1 = 0 · r₂ = 1⊕1⊕0 = 0
codeword 0111 001
| Dataword | Codeword (a₃a₂a₁a₀ r₂r₁r₀) |
|---|---|
0100 | 0100 011 |
0111 | 0111 001 |
1001 | 1001 011 |
1101 | 1101 000 |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
20 / 36
s₀ = b₂ ⊕ b₁ ⊕ b₀ ⊕ q₀
s₁ = b₃ ⊕ b₂ ⊕ b₁ ⊕ q₁
s₂ = b₁ ⊕ b₀ ⊕ b₃ ⊕ q₂
| Syndrome s₂s₁s₀ | 000 | 001 | 010 | 011 | 100 | 101 | 110 | 111 |
|---|---|---|---|---|---|---|---|---|
| Error in | none | q₀ | q₁ | b₂ | q₂ | b₀ | b₃ | b₁ |
| Sent | Received | s₂s₁s₀ | Action |
|---|---|---|---|
0100011 | 0100011 | 000 | accept 0100 |
0111001 | 0011001 | 011 | flip b₂ → 0111 |
1101000 | 1101010 | 010 | q₁ wrong; data 1101 OK |
1101000 | 0001000 | 101 | flip b₀ → 0000 ✗ |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
21 / 36
Interactive
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
22 / 36
Sender: 1001 000 ÷ 1011
1010 1011 ) 1001000 1011 ──── 0100 0000 ──── 1000 1011 ──── 0110 0000 ──── 110
Generator
1011: 4 bits → 3 zeros
r = 3
Remainder
from the division
110
Codeword
dataword + remainder
1001 110
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
23 / 36
No error: 1001110 ÷ 1011
1010 1011 ) 1001110 1011 ──── 0101 0000 ──── 1011 1011 ──── 0000 0000 ──── 000
Syndrome
000 → accept 1001
Error: 1000110 ÷ 1011
1011 1011 ) 1000110 1011 ──── 0111 0000 ──── 1111 1011 ──── 1000 1011 ──── 011
Syndrome
011 → discard
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
24 / 36
1011 = x³ + x + 1
1001 = x³ + 1
bit i (from the right, starting at 0) = 1 → term xi
| Name | Generator | Used in |
|---|---|---|
| CRC-8 | x⁸ + x² + x + 1 | ATM header |
| CRC-10 | x¹⁰ + x⁹ + x⁵ + x⁴ + x² + 1 | ATM AAL |
| CRC-16 | x¹⁶ + x¹² + x⁵ + 1 | HDLC |
| CRC-32 | x³² + x²⁶ + x²³ + x²² + x¹⁶ + x¹² + x¹¹ + x¹⁰ + x⁸ + x⁷ + x⁵ + x⁴ + x² + x + 1 | Ethernet, LANs |
| A good generator… | so that it detects |
|---|---|
| has more than one term | all single-bit errors |
| has (x + 1) as a factor | all odd numbers of errors |
| has degree r | all bursts of length ≤ r |
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
25 / 36
1101011011 0000 ÷ 10011
11010110110000 10011 ───── 01001110110000 10011 ───── 00000010110000 10011 ───── 00000000101000 10011 ───── 00000000001110
Grey bits are already zero; we XOR only under a leading 1
Generator
x⁴ + x + 1
10011 → r = 4
Remainder
1110
Transmitted
1101011011 1110
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
26 / 36
Interactive
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
27 / 36
| Step | Sender | Receiver |
|---|---|---|
| Data | 7, 11, 12, 0, 6 | 7, 11, 12, 0, 6, 9 |
| Sum | 36 = 10 0100 | 45 = 10 1101 |
| Wrap (4-bit) | 0100 + 10 = 0110 = 6 | 1101 + 10 = 1111 = 15 |
| Complement | 1001 = 9 (checksum) | 0000 = 0 → accept |
Ex. 10.11
plain sum
36
Ex. 10.12
wrapped sum
6
Ex. 10.13
complement 15 − 6
9
One's complement of a 4-bit number = flip every bit = 15 − x
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
28 / 36
“Forouzan” in ASCII, 16-bit words
Fo 466F ro 726F uz 757A an 616E ──── sum 1 8FC6 wrap 8FC6 + 1 8FC7 ¬ → 7038
Checksum sent
FFFF − 8FC7
7038
Receiver
8FC7 + 7038 = FFFF, complement
0000 ✓
Hex complement: subtract each digit from F
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
29 / 36
Interactive
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
30 / 36
| Code | Redundancy | Detects | Corrects | Where used |
|---|---|---|---|---|
| Simple parity | 1 bit | odd numbers of errors | no | serial (UART), memory |
| 2D parity | row + column | 1–3 errors, most bursts | 1 error | older storage, teaching |
| Hamming (7,4) | 3 per 4 bits | 2 errors | 1 error | ECC memory, FEC |
| CRC | r bits (8–32) | all bursts ≤ r, odd errors | no | Ethernet, Wi-Fi, HDLC |
| Checksum | 16 bits | most errors, not reordering | no | IP, TCP, UDP |
Rule of thumb: CRC in hardware at the link layer; checksum in software at higher layers; Hamming-type FEC where resending is impossible.
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
31 / 36
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
32 / 36
Click the video to play or pause · Press → for full screen
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
33 / 36
Checkpoint 3 · 3 minutes
Find the CRC for dataword 1010 with generator 1011, and the codeword sent.
Hamming (7,4) from the slides: received 0101011. Syndrome? Corrected dataword?
4-bit checksum of the words 5, 9, 14, 3. What does the receiver compute if nothing goes wrong?
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
35 / 36
Next lecture
Flow and error control: Stop-and-Wait, Go-Back-N and Selective Repeat ARQ, piggybacking, and the HDLC and PPP protocols.
Reading: Forouzan, Chapter 11 · Practise in the Error-Control Lab
Md Mahbub E Noor
Lecture 11: Data Link Layer I
University of Barishal
36 / 36