1. Home
  2. Computer Networks
  3. CRC (Cyclic Redundancy Check)

CRC (Cyclic Redundancy Check)

A few check bits that let the receiver spot damaged frames. CRC is long division with XOR, and the remainder becomes the check bits.

Interactive 3DIntermediate11 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

Noise can flip bits on a network cable or radio link. A CRC (cyclic redundancy check) adds a few check bits so the receiver can tell whether the frame arrived damaged. It is just long division with XOR. Press Compute CRC.

Pseudocode

  1. r ← degree of the generator (its length − 1)
  2. append r zeros to the data
  3. for each bit from the left (except the last r):
  4. if the bit is 1: XOR the generator under it
  5. (if it is 0: nothing to subtract, move on)
  6. remainder ← the last r bits; send data + remainder
Read this model as text · 16 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. Noise can flip bits on a network cable or radio link. A CRC (cyclic redundancy check) adds a few check bits so the receiver can tell whether the frame arrived damaged. It is just long division with XOR. Press Compute CRC.
  2. The sender wants to protect the data 100100. Sender and receiver agree on a generator 1101 (x³ + x² + 1). Its degree is r = 3, so the CRC will be 3 bits long.
  3. Append 3 zeros: 100100 → 100100000. That makes room for the CRC — like multiplying by x3.
  4. The leading bit (position 0) is 1, so put the generator 1101 under it and XOR (subtraction without borrows: 1 ⊕ 1 = 0, 1 ⊕ 0 = 1).
  5. 1001 ⊕ 1101 = 0100. The leading 1 always becomes 0, so the number keeps shrinking.
  6. The leading bit (position 1) is 1, so put the generator 1101 under it and XOR (subtraction without borrows: 1 ⊕ 1 = 0, 1 ⊕ 0 = 1).
  7. 1000 ⊕ 1101 = 0101. The leading 1 always becomes 0, so the number keeps shrinking.
  8. The leading bit (position 2) is 1, so put the generator 1101 under it and XOR (subtraction without borrows: 1 ⊕ 1 = 0, 1 ⊕ 0 = 1).
  9. 1010 ⊕ 1101 = 0111. The leading 1 always becomes 0, so the number keeps shrinking.
  10. The leading bit (position 3) is 1, so put the generator 1101 under it and XOR (subtraction without borrows: 1 ⊕ 1 = 0, 1 ⊕ 0 = 1).
  11. 1110 ⊕ 1101 = 0011. The leading 1 always becomes 0, so the number keeps shrinking.
  12. The leading bit (position 4) is 0: the generator does not "fit", so we XOR with zeros — nothing changes. Move one place right.
  13. The leading bit (position 5) is 1, so put the generator 1101 under it and XOR (subtraction without borrows: 1 ⊕ 1 = 0, 1 ⊕ 0 = 1).
  14. 1100 ⊕ 1101 = 0001. The leading 1 always becomes 0, so the number keeps shrinking.
  15. All 6 data positions are done. The last 3 bits are the remainder: 001. That is the CRC.
  16. The sender transmits 100100 followed by 001 = 100100001. This whole frame is now exactly divisible by 1101. Press Check at receiver.

Try this in the 3D model

  • Press Compute CRC for 100100 with generator 1101. How many times is the generator actually XORed?
  • Press Check at receiver. The remainder should be all zeros.
  • Press Flip a bit in transit, then Check at receiver again. What remainder do you get now?
  • Switch to the CRC-8 generator. How many check bits are added?

Why do we need error detection?

Bits travel as electrical signals, light or radio waves, and noise can flip a 0 into a 1. A single flipped bit can turn a payment of ₹100 into ₹228. The receiver needs a cheap way to ask: did this frame arrive exactly as it was sent?

A simple parity bit (an extra bit that makes the number of 1s even) catches one flipped bit but misses two. CRC adds a few more check bits, chosen so cleverly that it catches almost every error that happens in practice. That is why every Ethernet frame, Wi-Fi packet, ZIP file and PNG image carries one.

The idea: division with XOR

Treat the bits as the coefficients of a polynomial: 1101 means x³ + x² + 1. Sender and receiver agree on a generator polynomial G of degree r.

  1. Append r zeros to the data (this multiplies it by xʳ).
  2. Divide by the generator using mod-2 arithmetic: subtraction is just XOR, so there are no borrows and no carries.
  3. The remainder (r bits) is the CRC. Replace the appended zeros with it and send the frame.

Because the remainder was “added back”, the frame that is sent is exactly divisible by G.

Step by step: data 100100, generator 1101

The generator has degree 3, so we append 3 zeros: 100100000. Then, from the left, whenever the leading bit is 1 we XOR the generator underneath it:

Step Leading bit Working bits after the step
start – 100100000
1 1 → XOR 1101 010000000
2 1 → XOR 1101 001010000
3 1 → XOR 1101 000111000
4 1 → XOR 1101 000001100
5 0 → skip 000001100
6 1 → XOR 1101 000000001

The remainder is the last 3 bits, 001. The sender transmits 100100 001.

At the receiver

The receiver divides the whole received frame by the same generator (no zeros appended this time).

  • Remainder 0 → the frame is accepted.
  • Remainder not 0 → an error happened. The frame is dropped and sent again.

Flip any single bit of 100100001 and the remainder is no longer zero. Try it in the 3D model.

What does CRC catch?

With a well-chosen generator of degree r, a CRC detects:

  • every single-bit error,
  • every burst of errors no longer than r bits,
  • every odd number of flipped bits (if the generator has x + 1 as a factor),
  • almost every longer burst: only about 1 in 2ʳ slips through.

With CRC-32 (r = 32), used by Ethernet, that is about one undetected burst in four billion.

Name Generator (degree) Used in
CRC-8 x⁸ + x² + x + 1 small sensors, ATM headers
CRC-16 x¹⁶ + x¹⁵ + x² + 1 Modbus, USB
CRC-32 degree 32 Ethernet, Wi-Fi, ZIP, PNG, gzip

Code

def crc_remainder(data: str, gen: str) -> str:
    r = len(gen) - 1
    bits = list(data + "0" * r)              # append r zeros
    for i in range(len(data)):
        if bits[i] == "1":                   # leading 1: XOR the generator
            for j, g in enumerate(gen):
                bits[i + j] = "0" if bits[i + j] == g else "1"
    return "".join(bits[-r:])

def crc_check(frame: str, gen: str) -> bool:
    r = len(gen) - 1
    bits = list(frame)
    for i in range(len(frame) - r):
        if bits[i] == "1":
            for j, g in enumerate(gen):
                bits[i + j] = "0" if bits[i + j] == g else "1"
    return "1" not in bits[-r:]              # remainder must be 0

crc = crc_remainder("100100", "1101")
print(crc)                                   # 001
print(crc_check("100100" + crc, "1101"))     # True
print(crc_check("101100001", "1101"))        # False: one bit was flipped

import zlib
print(hex(zlib.crc32(b"hello")))             # 0x3610a686, the real CRC-32

Real implementations process a whole byte at a time using a 256-entry lookup table, and many CPUs even have a CRC instruction.

CRC vs other checks

Parity bit Checksum (sum of words) CRC Hamming code
Extra bits 1 16 r (8–32) ~log₂ n
Detects Odd number of flips Many errors Almost all errors 1–2 flips
Corrects ❌ ❌ ❌ ✅ single-bit errors
Used in Memory, serial ports IP, TCP, UDP headers Ethernet, Wi-Fi, files ECC memory

Common mistakes

  • Forgetting to append the r zeros at the sender, or appending them again at the receiver.
  • Subtracting with borrows. In CRC arithmetic, subtraction and addition are both XOR.
  • Getting the degree wrong. A generator of length 5 (like 10011) has degree 4, so the CRC has 4 bits.
  • XORing when the leading bit is 0. Then you “subtract” zero and simply move one place right.

Complexity at a glance

Case / operationTimeWhy
Computing the CRC bit by bitO(n · r)n data bits, an r-bit remainder.
Table-driven CRC (one byte at a time)O(n / 8) lookupsHow network cards and libraries really do it.
Extra bits sentrThe degree of the generator.
Bursts of errors always caughtlength ≤ rLonger bursts slip through with probability about 2^−r.

Quick check

Test yourself — pick an answer to see if you got it.

1. The data is 1101 and the generator is 1011 (degree 3). How many zeros are appended before dividing?

2. What does the receiver do with a received frame?

3. What is 1101 ⊕ 1011 (bitwise XOR)?

4. Can a CRC correct the error it finds?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in CRC (Cyclic Redundancy Check). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.