Sub topic 5
CRC: Generator Polynomials and Modulo-2 Division
This chapter assumes that you know binary numbers, bitwise XOR, and the roles of transmission and propagation delay. A cyclic redundancy check (CRC) is an error-detection method used by data-link protocols. It treats a frame as a polynomial over binary values and appends a short remainder to the data.
The CRC working model
A sender and receiver agree on a generator polynomial. In bit form, the polynomial is written with its highest-power coefficient first. For example, 10011 represents:
\[ x^4+x+1 \]
The generator has degree \(n=4\), so the CRC remainder contains four bits.
The sender performs these steps:
- Append \(n\) zero bits to the data.
- Divide the result by the generator using modulo-2 division.
- Use XOR instead of ordinary subtraction.
- Append the \(n\)-bit remainder to the original data.
The receiver divides the complete received frame by the same generator. A zero remainder means that the frame passes the CRC check. A nonzero remainder means that an error was detected.
Modulo-2 division
Modulo-2 arithmetic has no carries or borrows:
- \(0 \oplus 0=0\)
- \(1 \oplus 0=1\)
- \(1 \oplus 1=0\)
During division, align the generator with the leftmost remaining 1. XOR the aligned bits, then continue with the next 1.
For data 1101011111 and generator 10011, append four zeros:
11010111110000Modulo-2 long division produces the remainder 0010. Therefore, the transmitted codeword is:
11010111110010The remainder is always shorter than the generator. If the generator has \(n+1\) bits, the remainder has at most \(n\) bits, including leading zeros.
def crc_remainder(data: str, generator: str) -> str:
n = len(generator) - 1
bits = [int(bit) for bit in data + "0" * n]
gen = [int(bit) for bit in generator]
for i in range(len(data)):
if bits[i] == 1:
for j in range(len(gen)):
bits[i + j] ^= gen[j]
return "".join(str(bit) for bit in bits[-n:])
data = "1101011111"
generator = "10011"
print(crc_remainder(data, generator)) # 0010What CRC detects
A CRC detects every single-bit error when the generator has at least two nonzero terms. It also detects many multiple-bit errors and all burst errors whose length is no greater than the generator degree. Detection is not correction: after finding an invalid remainder, the receiver normally discards the frame and relies on retransmission.
> callout note > Do not use ordinary subtraction in CRC division. XOR is subtraction modulo 2, so there are no carries or borrows.
Exercise
A generator is 1011, and the data is 1101. How many zeros must the sender append before division, and how many bits are in the final CRC remainder?
Show answer
The generator has four bits, so its degree is 3. Append three zeros, and the final remainder has three bits.