Digital Electronics · lesson 11

Error Detection and Correction

Parity bits, why they can only detect and not correct, and a fully worked Hamming(7,4) example that both detects and corrects a single-bit error.

15 min read · Free, no login

Why stored and transmitted bits need protection

Every representation covered in this path has assumed the bits arrive intact. Real memory cells and real communication links do not make that guarantee โ€“ a cosmic ray can flip a stored bit, and electrical noise can flip a transmitted one. This lesson covers the standard techniques for noticing when a bit has flipped, and in one important case, for figuring out which bit it was and flipping it back without ever needing to ask for the data again.

The parity bit

The simplest protection scheme adds one extra bit to a word, chosen so that the total number of 1s in the word plus the parity bit is always even (even parity) or always odd (odd parity), by convention fixed in advance. For even parity, the parity bit is just the XOR of all the data bits, since XOR of a set of bits is 0 exactly when the set contains an even number of 1s. The receiver recomputes the same XOR over the received word; if it does not come out to the expected parity, at least one bit has flipped.

A single parity bit has a sharp limitation: it can only tell the receiver that an error happened, never which bit is wrong, so there is nothing for the receiver to do but ask for the data again. It also has a blind spot โ€“ if exactly two bits flip, the total count of 1s is unchanged, parity still checks out, and the error passes completely unnoticed. This is why parity by itself is reserved for links or memories where errors are rare and retransmission is cheap, and why anything that needs to recover from an error on the spot, without retransmission, needs more than one check bit.

Hamming codes: detecting and correcting

A Hamming code adds several parity bits, each covering a different, overlapping subset of the data bits, chosen so that the specific pattern of which checks fail and which pass points directly at which single bit is wrong. The classic Hamming(7,4) code takes 4 data bits and adds 3 parity bits to form a 7-bit codeword. The 7 positions are numbered 1 through 7, and the positions that are powers of two โ€“ 1, 2 and 4 โ€“ are reserved for the parity bits p1, p2 and p4; the remaining positions, 3, 5, 6 and 7, carry the four data bits in order.

Each parity bit covers exactly the positions whose binary representation has its own bit set: p1 (position 1, binary 001) covers every position whose binary form has bit 0 set โ€“ positions 1, 3, 5, 7. p2 (position 2, binary 010) covers positions with bit 1 set โ€“ 2, 3, 6, 7. p4 (position 4, binary 100) covers positions with bit 2 set โ€“ 4, 5, 6, 7. Every data position is covered by a different combination of the three parity bits, which is exactly the property that lets the decoder pin down a single flipped bit uniquely.

Worked example: encoding 1, 0, 1, 1

Let us encode the 4 data bits 1, 0, 1, 1, placed into positions 3, 5, 6, 7 respectively (d1=1 at position 3, d2=0 at position 5, d3=1 at position 6, d4=1 at position 7). Each parity bit is the XOR of its covered positions:

  • p1 = position 3 ⊕ position 5 ⊕ position 7 = 1 ⊕ 0 ⊕ 1 = 0
  • p2 = position 3 ⊕ position 6 ⊕ position 7 = 1 ⊕ 1 ⊕ 1 = 1
  • p4 = position 5 ⊕ position 6 ⊕ position 7 = 0 ⊕ 1 ⊕ 1 = 0

Laying out positions 1 through 7 in order (p1, p2, d1, p4, d2, d3, d4) gives the codeword 0 1 1 0 0 1 1.

pos 1 0 p1 pos 2 1 p2 pos 3 1 d1 pos 4 0 p4 pos 5 0 d2 pos 6 1 d3 pos 7 1 d4
The Hamming(7,4) codeword for data 1011: parity bits (bright outline) sit at positions 1, 2 and 4; data bits occupy the rest, in order.

Locating an error with the syndrome

Suppose position 5 flips in transmission, so the received word becomes 0 1 1 0 1 1 1 instead of 0 1 1 0 0 1 1. The decoder recomputes each parity check against the received bits and records whether it still passes (0) or now fails (1), building a 3-bit syndrome:

  • s1 = position 1 ⊕ position 3 ⊕ position 5 ⊕ position 7 = 0 ⊕ 1 ⊕ 1 ⊕ 1 = 1 (fails)
  • s2 = position 2 ⊕ position 3 ⊕ position 6 ⊕ position 7 = 1 ⊕ 1 ⊕ 1 ⊕ 1 = 0 (passes)
  • s4 = position 4 ⊕ position 5 ⊕ position 6 ⊕ position 7 = 0 ⊕ 1 ⊕ 1 ⊕ 1 = 1 (fails)

Reading the syndrome bits as s4s2s1 gives 1 0 1, which is binary for 5 โ€“ and the error really is at position 5. This is not a coincidence: it is exactly why the parity bits were assigned to cover the positions they cover. Each data position is covered by a unique combination of parity checks, so the pattern of which checks fail is the binary address of the one bit that flipped. Once the decoder has that address, correcting the error is just flipping the bit back at that position โ€“ no retransmission needed, which is the entire advantage a Hamming code buys over a plain parity bit.

Where this fits in a real design

Hamming(7,4) protects against exactly one flipped bit per codeword; two simultaneous flips can produce a syndrome that points at the wrong position and silently makes things worse, which is why error-correcting memory in real systems is usually described by its guarantee directly โ€“ SECDED (Single Error Correction, Double Error Detection) adds one more overall parity bit on top of a Hamming code specifically to at least detect, even where it cannot correct, a second simultaneous flip. Wider error-correcting codes used in storage and networking (Reed-Solomon, CRC for whole-packet detection without correction) extend the same core idea โ€“ add redundant, carefully chosen check bits so that the pattern of failed checks identifies the fault โ€“ to larger blocks and to error patterns beyond a single flipped bit.

Key takeaways

  • A single parity bit (the XOR of the data bits) detects any single flipped bit but cannot say which one, and misses any even number of simultaneous flips.
  • Hamming(7,4) places 3 parity bits at positions 1, 2 and 4, each covering a different overlapping subset of positions chosen from their binary representation.
  • The syndrome โ€“ the pattern of which parity checks fail โ€“ is the binary position number of the single flipped bit, letting the decoder correct it without retransmission.
  • Hamming(7,4) guarantees correction of exactly one flipped bit per codeword; protecting against two simultaneous flips needs an extra overall parity bit (SECDED) or a stronger code.