Sub topic 6
CRC: generator polynomials and modulo-2 division
This chapter assumes you know binary numbers and XOR, and that the data link layer must tell whether a received frame was corrupted. It also assumes you have seen parity and the Internet checksum. A cyclic redundancy check (CRC) is the stronger detector that Ethernet uses on every frame.
The working model
A CRC treats a bit string as a polynomial whose coefficients are 0 or 1. The bits 1101 are x³ + x² + 1. The sender and receiver agree on a fixed generator polynomial G(x) of degree r. Written as bits, G has r + 1 bits, and its first and last bits are both 1. For example, 10011 is x⁴ + x + 1, with r = 4.
The sender computes r check bits, called the remainder, and appends them to the data. The received frame is then exactly divisible by G(x) when no error occurred.
Arithmetic is done modulo 2. Addition and subtraction are both XOR, and there are no carries or borrows. Division is long division in which each "subtract" step is an XOR.
Sender procedure
- Let M be the data bits and r the degree of G.
- Append r zeros to M. This is M(x)·xʳ.
- Divide M(x)·xʳ by G(x) using modulo-2 division. Keep the r-bit remainder R.
- Transmit M followed by R. The transmitted polynomial is T(x) = M(x)·xʳ + R(x).
T(x) is divisible by G(x), because adding R (which equals subtracting it) cancels the remainder.
Worked example
The data is 1101011111 and G is 10011, so r = 4. The padded dividend is 11010111110000. At each step, if the leading bit of the current window is 1, XOR the window with 10011. If it is 0, slide one bit to the right.
- The window
11010XOR10011gives01001. The string is now01001 11110000. - Next window
10011XOR10011gives00000. The first six bits are now zero. - At position 6 the window is
11110. XOR gives01101. - At position 7 the window is
11010. XOR gives01001. - At position 8 the window is
10010. XOR gives00001. - The last four bits are
0010, which is the remainder.
The transmitted frame is 11010111110010.
def crc_remainder(data: str, generator: str) -> str:
n = len(generator) - 1
bits = [int(b) for b in data + "0" * n]
gen = [int(b) for b in generator]
for i in range(len(data)):
if bits[i]:
for j in range(len(gen)):
bits[i + j] ^= gen[j]
return "".join(str(b) for b in bits[-n:])
def crc_ok(frame: str, generator: str) -> bool:
n = len(generator) - 1
data, received = frame[:-n], frame[-n:]
return crc_remainder(data, generator) == received
frame = "1101011111" + crc_remainder("1101011111", "10011")
print(frame) # 11010111110010
print(crc_ok(frame, "10011")) # True
corrupted = frame[:3] + ("0" if frame[3] == "1" else "1") + frame[4:]
print(crc_ok(corrupted, "10011")) # FalseThe loop runs len(data) times. Each pass handles one leading bit, and the final r bits left in the list are the remainder. The check crc_ok recomputes the CRC over the data part and compares it with the received check bits. This is equivalent to dividing the whole frame and testing for a zero remainder.
Process at a glance
Why it detects errors
Suppose the channel adds an error pattern E(x), so the receiver sees T(x) + E(x) (an XOR). Since G divides T, the remainder equals E(x) mod G(x). The error goes undetected only when G(x) divides E(x). Properties of G therefore decide which errors are caught.
| Error type | Detected when |
|---|---|
| Any single-bit error | G has at least two nonzero terms, since xᵏ is never divisible by such a G |
| Any burst of length ≤ r | Always |
| Burst of length r + 1 | All but a fraction 2^−(r−1) of such bursts |
| Longer bursts | All but a fraction 2^−r of such bursts |
| Any odd number of bit errors | G(x) is divisible by (x + 1) |
A burst of length k means the first and last corrupted bits are k positions apart, and the bits between them may or may not be flipped.
CRC-32 is used in Ethernet. Its generator has 33 bits, with hexadecimal form 0x04C11DB7 once the leading x³² term is dropped. It produces a 32-bit frame check sequence (FCS). The CRC-16-CCITT generator is x¹⁶ + x¹² + x⁵ + 1.
Common errors
- Appending the wrong number of zeros. Append r zeros (generator length minus 1), not generator-length zeros.
- Using ordinary subtraction. Modulo-2 steps never borrow.
1 − 1,0 − 1and1 + 1are all XOR. - Dividing by a generator that starts with 0. The generator's leading bit must be 1. Dropping the leading term in hexadecimal notation, as in
0x04C11DB7, is a notation convention only. - Sending the remainder instead of data plus remainder. The frame is M followed by R. The receiver recomputes over the whole frame.
- Padding the remainder. If the remainder has leading zeros, keep them. The remainder is always exactly r bits, as in
0010. - Stopping the division early. Slide until all data bits have been processed. A window whose leading bit is 0 is XORed with nothing, so you just shift.
- Claiming CRC is a guarantee. CRC misses some error patterns, namely multiples of G(x). It also protects only against accidental noise, not deliberate tampering, because anyone can recompute it.
CRC compared with other detectors
Parity catches any odd number of flipped bits but misses many even-count patterns. The Internet checksum catches many random errors but can miss reordered words, because addition is commutative. CRC is sensitive to bit positions and handles burst errors well, with a fixed overhead of r bits. Its cost is a division, which is cheap in hardware: a shift register with XOR gates at the tap positions of G.
Exercise
Data is 1001 and the generator is 1011 (x³ + x + 1). Find the CRC remainder and the transmitted frame. Then say whether the receiver detects a single-bit error in position 2 of the frame.
Show answer
The generator has 4 bits, so r = 3 and the padded dividend is 1001000. Step 1: 1001 XOR 1011 = 0010, giving 0010000. Skip the zero bits; at position 2 the window is 1000, and 1000 XOR 1011 = 0011, giving 0000110. The last three bits are the remainder 110. The transmitted frame is 1001110. Check with polynomials: x⁶ + x³ mod (x³ + x + 1) = x² + x, which is 110. A single-bit error is always detected because G has more than one nonzero term, so the receiver gets a nonzero remainder and discards the frame.
Exercise
A 16-bit CRC (degree 16) is used. A burst error corrupts 16 consecutive bits. Is it always detected? What fraction of 17-bit bursts can escape detection?
Show answer
Yes. Every burst of length at most r = 16 is detected, because a nonzero polynomial of degree below 16 cannot be a multiple of G. A burst of length 17 escapes only when the error polynomial equals G itself, which happens for a fraction 2^−(r−1) = 2^−15 of such bursts.