2. Checksums & CRC
Compact fingerprints that catch errors
Beyond Single Parity
A single parity bit can detect a single-bit error, but it is blind to two-bit errors (they cancel out) and it cannot tell you where the error occurred. We need something better: a compact "fingerprint" of the data such that changing any bit changes the fingerprint. Checksums and CRCs are exactly that. They are the workhorses of error detection in networking, file formats, and storage — cheap to compute, small to transmit, and remarkably effective at catching corruption.
Simple Checksums
The simplest checksum: add up every byte, keep only the low 8 bits (or 16), and append the result to the data. The receiver re-sums all the bytes and compares.
Worked Example — 8-bit Checksum
Data bytes (ASCII for "Error"):
0x45 (E)
+ 0x72 (r)
+ 0x72 (r)
+ 0x6F (o)
+ 0x72 (r)
------
= 0x1BA
Keep only the low 8 bits: 0xBA. Append it to the
message. The transmitted stream is
[0x45, 0x72, 0x72, 0x6F, 0x72, 0xBA].
The receiver adds all six bytes: 0x45 + 0x72 + 0x72 + 0x6F +
0x72 + 0xBA = 0x274. The low 8 bits are 0x74
— not zero, so some implementations instead check that the
sum of the data bytes alone equals the appended checksum. Either
way, a mismatch means corruption.
Limitation: swapping two bytes produces the same sum.
[0x01, 0x02] and [0x02, 0x01] both sum to
0x03. A simple additive checksum cannot catch
reordering errors.
The Internet Checksum
IP, TCP, and UDP headers all use a 16-bit ones' complement checksum. The algorithm: treat the data as a sequence of 16-bit words, add them using ones' complement addition (end-around carry), then take the ones' complement of the result.
Worked Example — Tiny IP-style Header
Suppose we have four 16-bit words (checksum field set to 0x0000):
0x4500
+ 0x0034
+ 0x1A2B
+ 0x0000 (checksum field, zeroed)
---------
0x5F5F
No carry out of bit 16, so the partial sum stays 0x5F5F.
Ones' complement: flip every bit →
0xA0A0. That value is placed into the checksum
field.
The receiver sums all five words (including the checksum):
0x4500 + 0x0034 + 0x1A2B + 0xA0A0 = 0xFFFF.
In ones' complement, 0xFFFF is "negative
zero," which means no error.
Why Checksums Aren't Enough
Additive checksums, including the Internet checksum, miss several important error patterns:
- Byte swaps — reordering bytes within a word does not change the sum.
- Insertions and deletions — adding a zero byte does not change the sum.
- Burst errors — a burst that flips bits in a way that preserves the sum goes undetected.
We need a scheme with stronger mathematical guarantees — one that treats the data not as a bag of numbers but as a structured polynomial. Enter CRC.
CRC: Polynomial Division
The core idea behind CRC is elegant: treat the entire data block as one enormous polynomial, divide it by a fixed generator polynomial, and keep the remainder. That remainder is the CRC.
Think of ordinary long division: 1234 ÷ 7 gives quotient 176, remainder 2. The remainder is small, and it changes if you alter any digit of 1234. CRC works the same way, except the arithmetic is binary polynomial arithmetic instead of decimal.
Binary Polynomial Arithmetic
Polynomials with coefficients restricted to 0 or 1, using arithmetic modulo 2. The only operations are:
- Addition = XOR (0+0=0, 0+1=1, 1+1=0, no carry)
- Subtraction = also XOR (identical to addition in mod-2)
- Multiplication = shift and XOR
Addition Example
(x³ + x + 1) + (x³ + x²)
Coefficients: 1 0 1 1 (x³ + 0x² + x + 1)
XOR 1 1 0 0 (x³ + x² + 0x + 0)
= 0 1 1 1 (x² + x + 1)
Binary: 1011 XOR 1100 = 0111
No carries at any stage. Every "column" is independent.
Multiplication Example
(x² + 1) × (x + 1)
1 0 1 (x² + 1)
× 1 1 (x + 1)
------
1 0 1 (x² + 1) × 1
1 0 1 0 (x² + 1) × x, shifted left
--------
XOR 1 1 1 1 = x³ + x² + x + 1
CRC Step by Step
Data: 1101011 (7 bits)
Generator: 1011
(x³ + x + 1, degree 3)
Step 1 — Append r zeros
The generator has degree 3, so append 3 zeros to the data:
1101011000 (10 bits).
Step 2 — Binary Long Division (XOR-based)
1 1 0 0 0 0 1 ← quotient (we discard this)
--------------------
1 0 1 1 ) 1 1 0 1 0 1 1 0 0 0
1 0 1 1
-------
0 1 1 0 0
1 0 1 1
-------
0 1 1 1 1
1 0 1 1
-------
0 1 0 0 1
1 0 1 1
-------
0 0 1 0 0
0 0 0 0
-------
0 1 0 0 0
1 0 1 1
-------
0 0 1 1 0
0 0 0 0
-------
0 1 1 0 ← remainder = CRC
Step 3 — Append the CRC
The 3-bit remainder is 110. Replace the three appended
zeros with this remainder. The transmitted codeword is:
1101011110.
Step 4 — Verification
The receiver divides the entire received word
1101011110 by the generator 1011. If the
remainder is 000, no error is detected. Any non-zero
remainder signals corruption.
Why CRC Is Powerful
A CRC with a degree-r generator polynomial provides strong, mathematically provable detection guarantees:
| Error Pattern | Detected? |
|---|---|
| All single-bit errors | Yes (always, if generator has two or more terms) |
| All double-bit errors | Yes (if generator has a factor of suitable period) |
| All odd-number-of-bit errors | Yes (if generator is divisible by x+1) |
| All burst errors of length ≤ r | Yes (always) |
| Burst errors of length r+1 | Detected with probability 1 − 2−(r−1) |
| Longer bursts (> r+1) | Detected with probability 1 − 2−r |
For CRC-32 (r = 32), the probability of an undetected error in a random burst longer than 33 bits is roughly 1 in 4 billion.
Common CRC Polynomials
| Name | Width | Polynomial (hex) | Used In |
|---|---|---|---|
| CRC-8 | 8 bits | 0x07 | ATM header error check |
| CRC-16 | 16 bits | 0x8005 | Modbus, USB data packets |
| CRC-32 | 32 bits | 0x04C11DB7 | Ethernet, ZIP, PNG, gzip |
| CRC-32C | 32 bits | 0x1EDC6F41 | iSCSI, Btrfs, ext4 (optimized for hardware CRC instructions) |
CRC in Hardware
CRC-32 maps directly to a linear feedback shift register (LFSR): a chain of flip-flops with XOR gates at positions corresponding to the polynomial's terms. Each clock cycle shifts in one data bit and updates the register. The entire CRC computation requires no multiplier, no ALU — just XOR gates and a shift register.
LFSR Concept (CRC with generator x³ + x + 1)
data in
|
v
[⊕]--→[ r2 ]--→[ r1 ]--→[⊕]--→[ r0 ]--→ out
↑ |
└--------------------------┘
feedback
⊕ = XOR gate
r0, r1, r2 = register bits (the CRC accumulator)
Modern x86 CPUs include a dedicated CRC32 instruction
(part of SSE 4.2) that computes CRC-32C at effectively memory
bandwidth — several gigabytes per second. This is why CRC-32C
was chosen for modern storage systems like Btrfs and iSCSI.
Detection vs. Correction
What Checksums & CRC Do
- Detect that an error occurred
- Extremely high detection probability
- Very low overhead (32 bits protects megabytes)
- Fast — hardware or a few CPU instructions
What They Cannot Do
- Identify which bits are wrong
- Correct the data without retransmission
- Help when retransmission is impossible (deep space, failed disk)
- Provide cryptographic integrity (CRC is not a MAC)
When a CRC check fails, the standard response is retransmission: TCP asks for the segment again, Ethernet drops the frame and the higher layer retries, a disk controller re-reads the sector. For situations where retransmission is impossible or too slow — deep-space probes, archival storage, RAM — we need codes that actually correct errors. That is where Hamming codes come in.
Checksums & CRC in the Real World
| System | Check Used | What It Protects |
|---|---|---|
| Ethernet | CRC-32 | Every frame (FCS field, last 4 bytes) |
| TCP / IP / UDP | Internet checksum (16-bit) | Headers (IP), headers + payload (TCP/UDP) |
| ZIP / gzip / PNG | CRC-32 | Compressed data blocks |
| USB | CRC-5, CRC-16 | Token packets (CRC-5), data packets (CRC-16) |
| Bluetooth | CRC-24 | Baseband packets |
| SATA / NVMe | CRC-32C | Data integrity on the link layer |
| HDMI | CRC (per row) | Each pixel row of each video frame |
Key Takeaways
- A checksum adds up data values into a small fingerprint. Simple but blind to byte reordering.
- The Internet checksum (ones' complement sum) is fast and incrementally updatable, but weak against structured errors.
- CRC treats data as a binary polynomial and divides by a generator. The remainder is the check value.
- CRC arithmetic uses XOR (no carries), making it trivial to implement in hardware as a shift register.
- A degree-r CRC catches all bursts up to r bits and nearly all longer bursts.
- CRC-32 (Ethernet, ZIP) and CRC-32C (modern storage) are the two most common variants.
- Checksums and CRCs detect errors but cannot correct them — correction requires codes with more redundancy.
Try It Yourself — CRC Calculator
Enter a binary data string and a generator polynomial to see the full CRC computation: binary long division with XOR steps, the resulting remainder, and verification.
Binary Long Division
Result
Verification
Error Detection
Checksums and CRCs give us highly reliable error detection with minimal overhead. But detection alone means the only remedy is retransmission, and retransmission is not always possible. In the next page, we will see how Hamming codes achieve something remarkable: they detect and correct single-bit errors using just a handful of extra parity bits, with a clever arrangement that pinpoints the exact position of the flipped bit.