S3 · Lesson 39 · CRCs and error-correcting codes in hardware
LFSR and parallel CRCs, Hamming and SECDED codes, syndromes and interleaving
Every Ethernet frame ends with a 32-bit CRC, and every word in a server's memory carries 8 extra bits that correct any single flipped bit. Both are linear codes over bits, and both turn into XOR networks in hardware: a CRC is polynomial division done by a shift register, or many bits at a time by a wide XOR matrix; an ECC is a set of parity checks whose failures, read as a binary number, point at the bit to flip. This lesson builds both and puts numbers on what they catch.
- The line is the overhead of a SECDED code, the check bits as a fraction of the stored word, against the number of data bits m.
- Single-error correction needs r check bits with ; double-error detection adds one more.
- Wide words amortize the checks: 5 bits protect 8 (38% overhead), 8 protect 64 (11%), 11 protect 512 (2%).
Predict
How many check bits does a SECDED code need for 128-bit words?
Single-error correction needs the smallest r with : r = 7 gives 128 < 136, but r = 8 gives 256 ≥ 137. Detecting double errors as well takes one more overall parity bit: 9, a (137, 128) code with 6.6% overhead.
Name it
CRCs
A CRC treats the message as a polynomial over GF(2) and appends the remainder of dividing it by a generator polynomial of degree n. The serial circuit is a linear-feedback shift register that takes one bit per clock; a parallel CRC precomputes how w bits at once change the n-bit state, an XOR matrix, for a throughput of .
An n-bit CRC with a well-chosen generator detects:
- every single-bit error and every burst of length n or less;
- every odd number of errors, if divides the generator;
- other random corruption with probability .
Standard check values pin down a CRC's exact parameters: CRC-16/CCITT-FALSE (generator 0x1021, initial value 0xFFFF) gives 0x29B1 for the ASCII string "123456789".
Hamming codes
Put check bits at the power-of-two positions 1, 2, 4, …; check bit is the parity of every position whose index has bit j set. A single error at position k makes exactly the checks in k's binary expansion fail, so the syndrome, the failing checks read as a number, equals k. Correction is a decoder and an XOR.
SECDED adds an overall parity bit: a nonzero syndrome with odd overall parity is a single error (correct it); a nonzero syndrome with even parity is a double error (report it).
Interleaving
A particle strike can flip several adjacent cells. Interleaving the bits of k words across a row (k-way column multiplexing) puts adjacent cells in different words, so a burst of up to k bits causes at most one error per word.
Worked example
Encode the data 1011 in Hamming(7,4) (positions 1 to 7 hold p1, p2, d1, p4, d2, d3, d4), flip the bit at position 6, and decode.
Show the solution
- Parity bits. With : so the codeword is 0110011.
- Flip position 6. That gives 0110001.
- Syndrome. The checks over positions {1, 3, 5, 7}, {2, 3, 6, 7} and {4, 5, 6, 7} give 0, 1 and 1:
- Correct. Flipping bit 6 back restores 1011.
Answer the codeword is 0110011; after the flip the syndrome is 6, and correcting bit 6 recovers 1011.
Worked example
A 32-bit CRC engine handles 64 bits per clock at 400 MHz. What throughput is that, and how wide must it be for 100 Gb/s at the same clock?
Show the solution
- Throughput.
- Width for 100 Gb/s. It needs 250 bits per clock, so a 256-bit datapath.
- The cost. The XOR matrix grows with the width; very wide engines split the data into chunks and combine their CRCs.
Answer 25.6 Gb/s, and a 256-bit datapath for 100 Gb/s.
Worked example
A network sees a million corrupted frames a day, each with random errors. How often does one slip past its CRC-32?
Show the solution
- One frame's chance. Each corrupted frame passes with probability
- Per day. So a day get through: about one every 4300 days, 12 years.
- A 16-bit CRC. Under the same load it would let 15 through every day, which is why fast links use 32 bits.
Answer about one every 4300 days, roughly 12 years.
Check
A memory word holds 16 data bits protected by a Hamming code.
(a)
How many check bits does single-error correction need?
(b)
And SECDED?
(c)
What fraction of the stored word is check bits?
Check
A parallel CRC engine takes 32 bits per clock at 250 MHz.
(a)
What throughput does it reach?
Write units the way you normally would: 1.5 Gb/s, 1500 Mb/s and 0.0015 Tb/s are the same answer.
(b)
How many bits per clock would 100 Gb/s need?
Check
A link checks frames with a 16-bit CRC and sees 1000 randomly corrupted frames a day.
(a)
What is the chance a corrupted frame passes?
(b)
How many slip through in a year?
Check
A particle strike flips 3 adjacent cells in an SRAM row; the row interleaves 2 SECDED words bit by bit.
(a)
How many errors land in the worst-hit word?
(b)
What is the least interleaving that keeps every word correctable?
Check
Put a SECDED decoder's steps in order for a word read from memory.
- Check the overall parity of the whole word
- Recompute the check bits from the data bits read
- XOR them with the stored check bits to form the syndrome
- If the syndrome is nonzero and the parity is even, report an uncorrectable double error
- If the syndrome is nonzero and the parity is odd, flip the bit the syndrome names
Check
Why does an n-bit CRC catch every burst error n bits long or shorter?
Check
A block code stores m data bits with r check bits.
Step 1
What is its code rate?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What fraction of the stored bits is overhead?
Step 3
What is the most data bits a single-error-correcting Hamming code with r checks can protect?
Check
Write crc16(data): CRC-16/CCITT-FALSE of a bytes object (generator 0x1021, initial value 0xFFFF, most significant bit first, no final XOR), and ham_fix(c): given a 7-bit Hamming codeword as a list of bits in positions 1 to 7 (p1, p2, d1, p4, d2, d3, d4), correct any single error and return (data bits [d1, d2, d3, d4], syndrome).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
How does a Hamming code's syndrome point directly at the flipped bit?
The check bits sit at positions 1, 2, 4 and so on, and each covers exactly the positions whose binary index has its bit set: check 1 covers odd positions, check 2 covers positions with the 2's bit set, and so on. When the decoder recomputes the checks, a single error at position k disturbs precisely the checks corresponding to the 1 bits of k. Reading the failed checks as the bits of a binary number therefore reproduces k. A syndrome of zero means no single error, and any other value is the address of the bit to flip.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.