Contents / Information Theory / Linear and Hamming Codes
Chapter 8
Linear and Hamming Codes
Minimum distance, the bounds it must obey, and the classical linear constructions that correct errors by syndrome.
Introduction
Minimum distance, the bounds it must obey, and the classical linear constructions that correct errors by syndrome.
8.1Distance, Errors and Decoding
Shannon's theorem promises codes; it does not deliver them. This chapter and the next are about the codes themselves, and they start where coding theory started, with a geometric question: how do you place points in the cube so that noise moving a point a little cannot make it look like a different point?
The binary symmetric channel flips bits, so the natural measure of how much damage the channel has done is the number of positions that changed.
Definition 8.1 (Hamming weight and distance). For , the Hamming weight is the number of nonzero coordinates, and the Hamming distance is
Proposition 8.2 (Hamming distance is a metric). is a metric on : it is non-negative, vanishes only when , is symmetric, and satisfies the triangle inequality .
Proof. Only the triangle inequality needs an argument. If then cannot equal both and , so position is counted in or in (possibly both). Hence every position counted on the left is counted at least once on the right.∎
Metric structure is what licenses the geometric language — balls, packing, covering — used for the rest of the chapter.
Definition 8.3 (Code, rate, minimum distance). A code of length is a subset with codewords; its rate is . Its minimum distance is
A code of length , size and minimum distance is called an code.
Definition 8.4 (Nearest-neighbour decoding). The nearest-neighbour (minimum-distance) decoder maps a received word to a codeword minimising , breaking ties arbitrarily.
Proposition 8.5 (Nearest-neighbour decoding is maximum likelihood on the BSC). On with , the nearest-neighbour decoder is a maximum-likelihood decoder.
Proof. If is sent and received at distance , then
Since we have , so the likelihood is strictly decreasing in . Maximising the likelihood is therefore minimising .∎
Theorem 8.6 (Detection and correction capability). Let have minimum distance . Then
- detects every error pattern of weight at most ;
- corrects every error pattern of weight at most under nearest-neighbour decoding;
- both bounds are tight: some pattern of weight is undetectable and some pattern of weight is miscorrected.
Proof. (1) If is sent and an error of weight occurs, the received word is at distance from , so it cannot be another codeword (any two codewords are at distance ). Hence and the error is visible.
(2) Suppose is sent and . For any other codeword , the triangle inequality gives
so is the strictly closest codeword and the decoder returns it.
(3) Take at distance exactly . The error pattern has weight and carries to the codeword , which no detector can flag. For the second claim, change of the positions in which and differ, moving from to a word at distance from and from , so the decoder may prefer .∎
Intuition. Put a ball of radius around every codeword. Correction works exactly when the balls do not overlap, and the packing condition is the arithmetic of "no word is in two balls". Detection only needs the received word to fall outside every codeword other than the true one, which is a weaker demand — hence rather than .
Example 8.7 (The triple repetition code). Compute the parameters of and decode the received word .
Solution. Here , so , and ; it is a code of rate . It detects up to errors and corrects .
For : and , so nearest-neighbour decoding returns — the majority vote. If was actually sent, this is a miscorrection caused by a weight- error, exactly the tightness claimed in part (3) of the capability theorem.□
Pitfall. Minimum distance governs guaranteed capability, not typical behaviour. A code with corrects every single error and may well correct most double errors too; conversely, quoting an average distance says nothing, because one unusually close pair of codewords is enough to ruin the guarantee.
8.2Error Detection
Detection is cheaper than correction and is often all a system needs, because a retransmission mechanism can supply the correction (see the final sections of this chapter on ARQ).
Definition 8.8 (Detection capability). A code detects an error pattern if for every codeword . For a linear code this depends only on : the undetectable patterns are precisely the nonzero codewords.
Proposition 8.9 (Undetected error probability of a linear code). Let be a linear code with weight enumerator , used on . The probability that an error occurs and goes undetected is
Proof. By linearity an error is undetected exactly when the error pattern is a nonzero codeword, and on the BSC a fixed pattern of weight occurs with probability . Summing over the nonzero codewords and grouping by weight gives the formula; the sum starts at because there are no nonzero codewords of smaller weight.∎
The simplest detector adds a single parity bit.
Definition 8.10 (Single parity-check code). The single parity-check code consists of all with ; equivalently, a data word of length is extended by the bit making the total number of ones even.
Proposition 8.11 (What one parity bit buys). The single parity-check code has minimum distance . It detects every error pattern of odd weight and no error pattern of even weight.
Proof. The parity of the received word equals the parity of the error pattern, since the codeword's parity is zero. So the check fails exactly when is odd. Two coordinates can be flipped to move between codewords, and one cannot, so ; by the capability theorem it detects error, and in fact all odd-weight patterns.∎
Example 8.12 (Parity in action, and its blind spot). Send the data word with an even parity bit. Show a detected error and an undetected one.
Solution. The data word has three ones, an odd number, so the parity bit is and the codeword is (four ones, even).
Detected. Suppose the third bit flips: the receiver sees , which has three ones. Odd parity where even was expected, so an error is flagged. Every single flip changes the count of ones by exactly one and so always flips the parity.
Undetected. Suppose bits three and four both flip: the receiver sees , which has two ones — even. The check passes and the double error is delivered silently. This is the price of .□
Definition 8.13 (Cyclic redundancy check). Identify a bit string with the polynomial over . Fix a generator polynomial of degree with . The CRC encoder maps a message polynomial of degree to
which is divisible by . The receiver recomputes the remainder and flags an error if it is nonzero.
Theorem 8.14 (Burst-error detection by a CRC). A CRC with generator of degree detects every error burst of length at most , where a burst of length is an error polynomial of the form with and .
Proof. An error is undetected precisely when divides . Since , is not a factor of , and as is a unique factorisation domain forces . But and , so this is impossible. Hence every such burst is detected.∎
Corollary 8.15 (Odd-weight detection). If , the CRC detects every error pattern of odd weight.
Proof. An error polynomial has odd weight if and only if in . If , then , a contradiction. Practical generators such as CRC-32 are chosen with the factor for exactly this reason.∎
Intuition. A CRC is a checksum computed by long division instead of addition, and the divisor is chosen so that the error patterns real hardware produces — short bursts on a wire, on a disk track, in a packet — cannot be multiples of it. Its strength is not a distance guarantee but a deliberately awkward algebraic structure.
Example 8.16 (Computing a CRC remainder). With (bit pattern ) and message , compute the transmitted codeword.
Solution. Here , so form , that is the bit string , and divide by over (XOR long division):
; bring down: ; then ; then .
The remainder is , so the codeword is . The receiver dividing by gets remainder and accepts. Any burst of length in transit leaves a nonzero remainder, by the burst theorem.□
8.3Linear Codes, Syndromes and the Fundamental Bounds
Searching a general code of size for the nearest codeword costs comparisons, which is hopeless. Linearity is the structure that makes both encoding and decoding cheap, and essentially every code in use is linear.
Definition 8.17 (Linear code, generator and parity-check matrices). An linear code is a -dimensional subspace . A generator matrix is a matrix whose rows are a basis, so that . A parity-check matrix is an matrix of rank with
is systematic if , in which case and the first coordinates of a codeword are the message itself.
Proposition 8.18 (Minimum distance equals minimum weight). For a linear code, .
Proof. and ranges over all nonzero codewords as the pair ranges over distinct codewords, by closure under subtraction.∎
This halves the work of computing — one scans codewords rather than pairs — and there is a better criterion still, read off the parity-check matrix.
Theorem 8.19 (Distance from the parity-check matrix). A linear code with parity-check matrix has minimum distance if and only if every set of columns of is linearly independent and some set of columns is dependent.
Proof. A word of weight with support satisfies exactly when the columns indexed by sum to zero, i.e. are linearly dependent (over , a dependency is a subset summing to zero). So a nonzero codeword of weight exists precisely when some columns are dependent. Taking the least such and applying the previous proposition gives the statement.∎
Definition 8.20 (Syndrome). The syndrome of a received word is .
Proposition 8.21 (The syndrome sees only the error). If with , then . Two words have the same syndrome if and only if they lie in the same coset of .
Proof. . And iff iff iff are in the same coset.∎
Method 8.22 (Standard-array (coset-leader) decoding).
- List the cosets of in .
- In each coset pick a minimum-weight element, the coset leader ; store it against the coset's syndrome .
- To decode : compute , look up , and output .
Proposition 8.23 (Coset-leader decoding is nearest-neighbour decoding). The output of standard-array decoding is a codeword at minimum distance from .
Proof. Candidates for the error are exactly the members of the coset , since must be a codeword. Choosing of least weight in that coset therefore minimises .∎
The table has entries rather than codewords, so syndrome decoding is cheap precisely when the code has few parity checks — and this is the structure that Hamming codes exploit to the limit.
Example 8.24 (Syndrome decoding the repetition code). Decode in the repetition code, whose parity-check matrix is .
Solution. The two rows encode and , which force , so the null space is indeed .
Compute . The columns of are , and matches column , so the minimum-weight error with this syndrome is . Then , agreeing with the majority vote as the previous proposition guarantees.□
How good can a code be? Three bounds frame the answer: two say that distance is expensive, one says that a decent distance is always available.
Lemma 8.25 (Volume of a Hamming ball). The ball has elements, and for ,
Proof. The count is immediate: choose which positions differ. For the upper bound, with ,
where the second inequality uses that is decreasing in for . The lower bound follows because is the largest of the binomial coefficients and by Stirling.∎
Theorem 8.26 (Hamming (sphere-packing) bound). Any binary code of length , size and minimum distance satisfies
A code attaining the bound with equality is called perfect.
Proof. By the capability theorem the balls of radius about distinct codewords are pairwise disjoint (a word in two such balls would be within of two codewords at distance , contradicting the triangle inequality). Each ball contains words and they all sit inside , so .∎
Theorem 8.27 (Singleton bound). Any code of length , size and minimum distance satisfies .
Proof. Delete the last coordinates of every codeword. Two distinct codewords differ in at least positions, so they still differ somewhere among the remaining coordinates; the projection is therefore injective. Hence , i.e. .∎
Theorem 8.28 (Gilbert–Varshamov bound). For any and there exists a binary code of length and minimum distance at least with
Proof. Greedy construction. Start with an empty code and repeatedly add any word of at distance from every word already chosen, stopping when no such word exists. At that point the balls of radius about the chosen words cover — otherwise an uncovered word could be added — so .∎
Corollary 8.29 (Asymptotic form). For relative distance fixed with , there exist codes of rate , while the Hamming bound forces .
Proof. Insert the ball-volume estimates of the volume lemma into the two previous theorems and take logarithms divided by .∎
Intuition. The three bounds are a packing problem in disguise. Singleton is a coordinate-counting argument and is usually loose in the binary case; Hamming says the balls must fit; Gilbert–Varshamov says that if you place points greedily you cannot get stuck too early, because a stuck configuration must cover the whole space. The gap between the Gilbert–Varshamov curve and the Hamming curve has resisted closure for binary codes since 1952 — the best possible rate–distance trade-off is still unknown.
Example 8.30 (Can a single-error-correcting code exist?). Check the Hamming bound for , , .
Solution. and , so . The bound holds with equality, so such a code — if it exists — is perfect. It does exist: it is the Hamming code with , built in the next section.□
Pitfall. Perfect codes are rare, not ideal. The complete list of binary perfect codes is: the trivial codes (, and the repetition codes of odd length), the Hamming codes, and the Golay code. "Perfect" means the packing has no wasted space, not that the code is a good choice — long Hamming codes are perfect and have relative distance tending to .
8.4Hamming Codes: Construction and Parameters
Richard Hamming, frustrated in 1947 by a relay computer that halted every weekend on a detected but uncorrected error, asked why a machine clever enough to know it was wrong could not work out what the right answer was. The family of codes he published in 1950 is the answer, and it is optimal in the exact sense of the sphere-packing bound.
The design idea is to make the syndrome do double duty: not merely announce that an error occurred, but spell out its position in binary.
Definition 8.31 (Binary Hamming code). For , the Hamming code is the linear code of length whose parity-check matrix is the matrix whose columns are all the distinct nonzero vectors of . When the columns are ordered so that column is the binary representation of , the code is in position-ordered form.
Theorem 8.32 (Parameters of the Hamming code). is an code with
so it corrects one error, and its rate is as .
Proof. has rows and contains all weight-one columns, so its rank is and the null space has dimension ; that is .
For the distance, use the column criterion. No single column is zero (all columns are nonzero), so . No two columns are equal, so no two are dependent and . Finally, any three distinct nonzero columns and — and is nonzero and distinct from both, so it is a column — sum to zero, exhibiting a dependent triple. Hence exactly.∎
Theorem 8.33 (Hamming codes are perfect). attains the Hamming bound with equality: the balls of radius about its codewords partition .
Proof. , since . Equality in the sphere-packing bound means the disjoint balls exhaust the space.∎
Intuition. With parity bits there are nonzero syndromes, and there are exactly possible single-error positions in a word of that length. Hamming's construction matches the two sets up perfectly, leaving no syndrome unused and no error position unaddressed. That exact fit is what "perfect" means here.
Example 8.34 (The Hamming code). Write down the parameters and a parity-check matrix for , and verify perfection.
Solution. Parameters: , , , , rate .
Position-ordered parity-check matrix, with column the binary form of (most significant bit on top):
Perfection: there are codewords, each owning a ball of words, and fills the space exactly.□
Remark (The dual: the simplex code). The dual code , generated by the rows of , is the simplex code: every nonzero codeword has the same weight , and all pairs of codewords are equidistant — hence the name. Its punctured extension is the first-order Reed–Muller code used by Mariner 9 to return pictures of Mars. Hamming codes have very high rate and small distance; their duals have very low rate and enormous distance. They are the two extremes of the same construction.
Pitfall. The rate of tends to , which sounds excellent until one notices that the relative distance tends to . On a BSC with fixed , a long Hamming code fails almost surely, because the expected number of errors greatly exceeds . Hamming codes are good for very clean channels, not for noisy ones.
8.5Syndrome Decoding of a Hamming Code
For a general linear code, syndrome decoding needs a stored table of coset leaders. For Hamming codes the table is unnecessary: the syndrome is the address.
Theorem 8.35 (Syndrome equals error position). Let be in position-ordered form and let with . If then ; if is the unit vector in position , then is the binary representation of .
Proof. by the syndrome proposition, and times a unit vector in position is the -th column of , which in position-ordered form is the binary representation of . The zero-error case is immediate since codewords are in the null space.∎
Method 8.36 (Decoding a Hamming code).
- Compute over .
- If , output unchanged.
- Otherwise read as an integer and flip bit of .
The whole decoder is parity computations and one bit flip, which is why Hamming codes survive in memory controllers where the decoding latency budget is a few gate delays.
Proposition 8.37 (Behaviour under two errors). If , the decoder of the recipe above always produces a codeword different from the one sent; it never reports failure.
Proof. Since and the code is perfect, every word of lies in exactly one radius- ball. A word at distance from the sent codeword lies in the ball of some other codeword with , so the decoder "corrects" to . There is no syndrome left over to signal failure — perfection has no spare room for a "detect only" region.∎
Example 8.38 (Decoding a single error). In the position-ordered code, the all-zero codeword is sent and position is flipped, so . Decode it.
Solution. selects the columns where has a one and adds them; only position is set, so is column , which is . Reading , flip bit of to recover . Recomputing the syndrome gives , confirming a valid codeword.□
Example 8.39 (Decoding a double error, and being misled). The same code, the same all-zero codeword, but positions and flip: .
Solution. The syndrome is the sum of columns and : . The decoder flips bit , producing the word — a codeword, but not the one sent, and at distance from it. The decoder reports success. This silent miscorrection is exactly what the previous proposition predicted, and it is what the extended code in the next section is designed to prevent.□
Intuition. Each parity check of the position-ordered Hamming code covers the positions whose binary address has a one in a given bit. A single flip breaks precisely those checks whose bit is set in its address, so the pattern of failed checks spells the address in binary. It is a binary search executed in parallel.
8.6Extended Hamming Codes and SECDED
The defect of a perfect code is that it has nowhere to put an "I do not know". One extra bit creates that room.
Definition 8.40 (Extended Hamming code). The extended Hamming code is obtained from by appending an overall parity bit . Its parity-check matrix is
with the all-ones row checking overall parity.
Theorem 8.41 (Parameters of the extended code). is a code: the length grows by one, the dimension is unchanged, and the minimum distance rises from to .
Proof. Every codeword of has even weight by construction. A word of of weight therefore extends to a word of weight , and no nonzero extended codeword can have weight or : weight would violate even parity together with , and weight would project to a word of weight in , of which there are none nonzero. Hence , attained. The dimension is unchanged because the appended bit is a function of the others.∎
Corollary 8.42 (SECDED). corrects any single error and simultaneously detects any double error: single error correction, double error detection.
Proof. Write for the overall-parity bit and the Hamming syndrome. If , then and . A single error has odd weight, so , and addresses it (or is zero if the flipped bit was the overall parity bit itself). A double error has even weight, so , while because means no weight-2 pattern is a codeword. So the three cases are distinguished by the pair :
Intuition. The overall parity bit answers one question the Hamming syndrome cannot: was the number of errors odd or even? If the syndrome points somewhere but the parity says "even number of flips", the pointer is lying and the controller should raise an alarm instead of corrupting data silently.
Example 8.43 (ECC memory). A SECDED code is the standard for server DRAM: data bits and check bits. Explain the parameters and what the controller does in each syndrome case.
Solution. Take : is , and its extension is . Shortening by deleting information coordinates (fixing them to zero and removing them) leaves a code, which is what memory modules implement; shortening never decreases the minimum distance, since the shortened code is a subset of the original restricted to a coordinate subset.
On a read: syndrome zero means deliver the data; odd overall parity with a nonzero pointer means flip the indicated bit, deliver the data, and log a correctable error; even overall parity with a nonzero pointer means two or more bits are wrong, and the controller raises an uncorrectable-error machine check rather than delivering wrong data. The last case is the whole reason for the extra bit.□
Pitfall. SECDED does not correct two errors; the extra distance buys detection only. And with three or more errors even the detection guarantee fails — a weight- pattern can be a codeword, delivering corrupted data with a clean syndrome. Chipkill and similar schemes exist because DRAM failures are not independent single bits.
- Treating detection and correction as the same capability: a parity bit says *that* something is wrong, never *where*. Correction needs enough distance to identify the error pattern, which costs roughly twice as much.
- **Forgetting linearity when computing **: for a linear code the minimum distance is the minimum nonzero *weight*, and the column condition on gives it faster still.
- Assuming errors are independent: real media produce bursts. A code designed for the BSC needs interleaving before it faces a bursty channel, which is why CRCs and interleavers appear together in standards.
- **Misreading the Singleton bound as achievable over **: binary MDS codes attaining are only the trivial ones; Reed–Solomon codes attain it over larger alphabets.
- **Thinking a bigger is always better**: raising costs rate, and by the Hamming bound the cost is roughly per symbol. Systems balance against rate, latency and the actual error statistics.
- Confusing the coset leader with the true error: syndrome decoding returns the *most likely* error consistent with the syndrome. When the true error is heavier than the leader, the decoder confidently outputs the wrong codeword.
- Expecting a Hamming code to correct two errors: it has , so . Worse, being perfect, it miscorrects double errors *silently* rather than reporting failure.
- Forgetting the position ordering: the syndrome-is-the-address property holds only when the columns of are the binary representations in order. Any column permutation gives an equivalent code, but then a lookup table is needed.
- Confusing rate with reliability: the rate of tends to while its relative distance tends to . Long Hamming codes are fast and fragile.
- Believing perfect codes are plentiful: over the only ones are the trivial codes, the odd-length repetition codes, the Hamming codes and the Golay code.
- Mixing up extension and shortening: extending appends a parity coordinate and raises from to ; shortening deletes information coordinates, lowering and together while leaving at least as large.
- Assuming modern systems rely on Hamming codes for the hard cases: they are used where blocks are tiny and latency is measured in gate delays. Anything trying to approach capacity uses LDPC, turbo or polar codes.