Computation

Finding the error without reading the message

Three parity checks on a seven-bit word produce three bits. If they are all zero nothing is wrong; otherwise they are the number of the position that broke. The message is never consulted, because the answer does not depend on it.

Worth reading first: Sixteen spheres that fill a cube.

A code that corrects one error can always be decoded by comparing the received word against every codeword and keeping the nearest. That works, and it is the wrong way to do it.

The syndrome of 1011010, and the bit it namesA parity-check matrix over a received word, with the resulting syndrome matched against the table of single-error syndromes.received1011010H111010001110101101001010synsyndromethe bit it names101bit 1111bit 2110bit 3011bit 4100bit 5010bit 6001bit 7000no error1011010 gives syndrome 010, which is column 6 — so bit 6 is wrong and 1011000 is thecodewordthe syndrome is three bits and the message is four: the check finds the error without everrecovering what was sent
Fig. 1 A received word run through the three parity checks. The result is 010, which is the sixth column of the matrix, so bit 6 is the broken one and 1011000 is what was sent. No codeword was consulted anywhere in that sentence.

The right way costs three exclusive-ors and involves no search at all, and the reason it works is a single observation about linearity.

The check, as a matrix

The three parity equations of the previous rung can be written as a matrix over the two-element field, with one row per check and one column per position. A one in row rr, column cc means that position cc takes part in check rr.

Multiplying that matrix by a word — which over this field means, for each row, adding up the bits in the positions the row marks — produces three bits. Call the result the syndrome of the word.

Addition here is arithmetic modulo two, which is what makes “the bits in these positions sum to zero” a linear equation rather than a condition needing cases.

A codeword is exactly a word whose three parity equations balance, so:

The syndrome of a codeword is 000000, and only codewords have syndrome 000000.

That is the definition of the code restated, and it is already useful: checking membership costs three additions rather than a comparison against sixteen candidates.

The observation that does the work

Suppose a codeword cc is sent and arrives as r=c+er = c + e, where ee is the pattern of flipped bits. The syndrome is computed by a linear operation, so

syn(r)=syn(c+e)=syn(c)+syn(e)=0+syn(e)=syn(e).\text{syn}(r) = \text{syn}(c + e) = \text{syn}(c) + \text{syn}(e) = 0 + \text{syn}(e) = \text{syn}(e).

The syndrome depends on the error and not on the message. Two different codewords corrupted in the same position produce the same three bits.

That is the whole trick, and it is worth sitting with, because it is unusual. The receiver computes a quantity from the received word that contains no information about what was sent — only about what went wrong. The message has been thrown away, deliberately, and what is left is exactly the part needed.

Nothing in the calculation ever recovers the four data bits. It does not need to.

Why the arithmetic is a field and not merely bits

The step above used linearity twice and it is worth being explicit about where that comes from, because the same move is what makes the last rung of this ladder possible over a much bigger alphabet.

Bits with exclusive-or as addition and the and gate as multiplication form the two-element field: addition and multiplication are defined, both are associative and commutative, multiplication distributes over addition, and the only non-zero element has a reciprocal, namely itself. Everything linear algebra does over the real numbers it does here — matrices, null spaces, dimension, rank — with no changes at all.

A code defined by parity equations is therefore the null space of a matrix, and a null space is a subspace. That is where the linearity comes from, where the coset structure comes from, and where the count of codewords comes from: a subspace cut out by three independent equations in a seven-dimensional space has dimension four, and 24=162^4 = 16.

The whole of this essay is the observation that a linear map applied to c+ec+e splits. That is an unremarkable fact about linear maps. What makes it striking is that the domain is messages, the map is a checksum, and the splitting means a checksum can be about an error without being about anything else. The generalisation to a field with more than two elements costs nothing, and buys the last rung of this ladder.

Three bits, seven positions, and one to spare

Now the design condition from the previous essay pays off.

The columns of the check matrix are the seven non-zero three-bit patterns, each appearing once. If the error is a single flip at position ii, then ee has a one in position ii and zeros elsewhere, so the syndrome is the ii-th column.

Since the columns are all different, the syndrome names the position. There is a table with seven rows, it is a bijection between the seven non-zero syndromes and the seven positions, and looking up a three-bit pattern is not a search in any meaningful sense.

How many words a code of distance 3 can holdA table of word lengths with the size of a correction ball and the maximum number of codewords the bound allows.nball size2ⁿ / ballexact?342.000yes — 2¹453.200no565.333no679.143no7816.000yes — 2⁴8928.444no91051.200no101193.091no1112170.667no1213315.077no1314585.143no14151092.3no15162048.0yes — 2¹¹balls of radius 1 hold 4–16 words, and 2ⁿ divided by that is the most codewordsdistance 3 allowsthe division comes out exactly at n = 3, 7, 15 — the only lengths where a perfectcode is arithmetically possible
Fig. 2 Why seven and not eight. Three check bits give eight syndrome values, one of which must mean “nothing wrong”, leaving seven for positions — and 2ⁿ divided by n + 1 is a whole power of two only when n + 1 is.

The counting is exactly tight. Three bits have eight values. There are seven positions that could break, plus the case of nothing breaking: eight outcomes. Not one value of the syndrome is wasted, which is another way of seeing that the code is perfect — a spare syndrome value would be a class of words the decoder had no story for.

The syndrome of 1011011, and the bit it namesA parity-check matrix over a received word, with the resulting syndrome matched against the table of single-error syndromes.received1011011H111010001110101101001011synsyndromethe bit it names101bit 1111bit 2110bit 3011bit 4100bit 5010bit 6001bit 7000no error1011011 gives syndrome 011, which is column 4 — so bit 4 is wrong and 1010011 is thecodewordthe syndrome is three bits and the message is four: the check finds the error without everrecovering what was sent
Fig. 3 The same machinery on a word differing from the last one in its final bit. The syndrome is 011 this time, which is column 4, so a different position is named — and 1010011 is the codeword it repairs to.
The syndrome of 1101001, and the bit it namesA parity-check matrix over a received word, with the resulting syndrome matched against the table of single-error syndromes.received1101001H111010001110101101001000synsyndromethe bit it names101bit 1111bit 2110bit 3011bit 4100bit 5010bit 6001bit 7000no error1101001 passes all three checks: syndrome 000, nothing to repairthe syndrome is three bits and the message is four: the check finds the error without everrecovering what was sent
Fig. 4 And a word that arrived intact. All three checks balance, the syndrome is 000, and there is nothing to repair. The same three additions answer both questions.

Checked over the whole space

The claim “every word is repaired correctly” is checkable in full, and the figures check it.

For each of the one hundred and twenty-eight seven-bit words, the generator computes the syndrome, looks up the position it names, flips that bit, and asserts that the result is a codeword. All one hundred and twenty-eight pass.

That is not a sample and it is not an argument; it is the statement itself, evaluated. The alternative — quoting the theorem that the columns being distinct implies correct decoding — would be quoting a proof of the thing the figure is supposed to be showing.

The whole space of seven-bit words, split into eight classesEight columns of sixteen words each, one column per syndrome, with the lightest word of each column marked.every seven-bit word, filed by the syndrome it produces0000000000000000000010110010110010110001100011000101101100011000100011101010011101110101001110101001111010011110100111111100100000010000001000101001100001000100001110001001101010010110100000101110101101101100111000110111011100111111101011111110010000001000000100001001001010011000000100101011100010011001010001010111001100111000111101101000111111101011111011011111010110001000000100000000110100100101000000101010110010100011011000010011110011100110011011101010010111110110111111100111011110000001000000100001001001010001000001001100101000111001010111000000011110110101101110011001100111110101011111011011111011101100000010000000000101001100001000100001110001001101010010110100100101110101101101100111000110111011100111111101001111111100010000001000000001100100001100100000011010101010100001111001000011011011110010101011110010011011110111101111001110111111101000000100000000110000100011000010000011100110101001001101010001010110110110110010111110000111101110111011100111011111128 words, 8 classes of 16 — one class per syndrome, and each has exactly one lightest memberthose eight leaders are the zero word and the seven single errors: nothing heavier is ever the best explanation
Fig. 5 The same verification laid out as a partition. Every word is filed by its syndrome, each class holds sixteen words, and each class has exactly one lightest member — which is the error the decoder subtracts.

The partition is the general statement, and the general statement is worth having because it survives when perfection does not. For any linear code, the received words split into classes by syndrome; each class is a shifted copy of the code; and decoding means subtracting the class’s lightest member, its coset leader. What is special about the Hamming code is that every coset leader has weight at most one, so the table is short and the correction is a single flip.

For a code that is not perfect the table is longer and some cosets have several equally light leaders, which is what an undecodable word looks like from the inside.

The cost, counted

It is worth putting the two decoding procedures side by side, since the whole point of the syndrome is that it replaces one with the other.

By nearest codeword. Compare the received word against each of the sixteen codewords, counting differences: sixteen comparisons of seven bits each, and the count grows with the number of codewords — which grows exponentially with the message length. At a length of 255 with 247 data bits, the number of codewords is larger than the number of atoms in anything.

By syndrome. Three exclusive-ors and one table lookup with seven entries. The work grows with the number of check bits, which is the logarithm of the number of codewords.

The gap between those two is not an optimisation; it is the difference between a procedure that can be run and one that cannot. And the reason it exists is entirely structural: the code is a subspace, so it can be described by the equations it satisfies instead of by the list of its members, and the equations are exponentially shorter than the list.

That is a pattern well beyond coding. A set defined by a short condition can be searched by checking the condition; a set defined by enumeration can only be searched by enumerating. The same distinction is what makes a matrix a workable description of an object far larger than itself.

Hamming’s own numbering

There is a version of this code in which the syndrome does not need a lookup table at all, and it is how Hamming presented it in 1950.

Number the positions 11 to 77. Put the parity bits at positions 11, 22 and 44 — the powers of two — and the data bits in the rest. Let check kk cover every position whose number has a one in bit kk of its binary numeral.

Then the column belonging to position ii is the binary numeral of ii, and the syndrome comes out as the position number written in binary. Syndrome 101101 means position five. There is no table; the answer is the arithmetic.

That arrangement is the same code as the one drawn here up to a permutation of the positions, and the permutation changes nothing about distance, weights or perfection. It is chosen here in the systematic form — data first, parity after — because that form makes the four message bits visible in the figures, and Hamming’s numbering scatters them.

Both arrangements make the same point in the end, which is that the check matrix’s columns are a numbering of the positions. Hamming’s makes it impossible to miss.

The weighing puzzle underneath

The structure has a familiar shape once it is stated as a question rather than as a matrix.

Three yes-or-no questions distinguish at most eight possibilities. There are eight possibilities here — seven broken positions and none. So the questions must be chosen so that every possibility gives a different pattern of answers, and each question must be asked of about half the positions.

That is the same counting that governs the pigeonhole principle run in reverse: with fewer than three questions there are more outcomes than answers and two of them must collide, so no scheme with two parity bits can locate an error among seven positions. Three is not merely sufficient; it is necessary.

The same counting appears wherever a fixed number of yes-or-no answers has to separate a fixed number of cases, and it is the reason a check digit over ten symbols can detect an error and not locate one: a single digit has ten values, and locating an error among ten positions each of which could be any of nine wrong values needs far more room than that.

The classical version is the counterfeit-coin puzzle — find the light coin among twelve in three weighings — and the solution has exactly this structure: each coin is assigned a pattern of which weighings it participates in, and the patterns must be distinct. The Hamming code is that puzzle with two outcomes per question instead of three, and its length is 2312^3-1 for the same reason the coin puzzle’s is (333)/2(3^3-3)/2.

The sixteen words of the [7,4] Hamming codeA table of sixteen seven-bit codewords with their data bits, parity bits and weights.four data bits, three parity bitsdataparityweight00000000000101130010110300111014010011140101100301100013011101041000101310011104101001141011000311000103110100141110100411111117sixteen codewords of length seven, minimum distance 3 — measured over all 120pairsweights: 1×0 · 7×3 · 7×4 · 1×7, and the lightest non-zero one weighs 3
Fig. 6 The sixteen codewords again, for reference. Nothing in the decoding above ever looked at this list — the syndrome named a position without knowing which of the sixteen was sent, which is what makes it cheap.

What it cannot do

The tightness of the count is also the limitation, and it is the same one the previous essay recorded.

If two bits flip, the syndrome is the sum of two columns, which is some other non-zero column — because the columns are all seven non-zero patterns and the sum of two distinct ones is a third. So a double error produces a perfectly ordinary syndrome naming a perfectly ordinary position, and the decoder flips a third bit, producing a word at distance three from what was sent. It fails silently, confidently, and by making things worse.

There is no defence within this code. Every syndrome value is spoken for, so there is no pattern the decoder could interpret as something is wrong here that cannot be fixed. Buying that requires a spare syndrome, which requires a bit — the extended [8,4][8,4] code, whose overall parity check is zero for codewords, one for single errors and zero again for double errors, splitting the cases apart.

A code on the 3-cube, and the balls around its wordsThe corners of a hypercube with the chosen codewords marked and the words within one error of each shaded.0000010100111001011101112 codewords in the 3-cube, minimum distance 3 — 1 error corrected, 2 detectedthe 2 balls of radius 1 hold 4 words each and cover all 8 exactly once: the code is perfect
Fig. 7 The same failure at a size that can be drawn. Two codewords in the 3-cube: a single error stays in the right ball, and a double error lands in the other one, where it is indistinguishable from a single error the other way.

That small figure is the honest picture of the failure. The word 110110 is one step from 111111 and two steps from 000000; a decoder seeing it repairs to 111111, and it is right to, given what it knows. Being wrong about a double error is not a defect of the decoding rule. It is what choosing the nearest codeword means when the actual codeword is not the nearest.

Where the code came from

The origin is worth a paragraph because it is unusually well documented and unusually mundane.

Hamming was at Bell Labs with weekend access to a relay machine that ran unattended. It checked parity on every word, and on a parity error it stopped and moved to the next job in the queue. Coming back on Monday to find that two days of computation had been abandoned on the first bad bit, he asked the obvious question: if the machine can tell that something is wrong, why can it not tell what is wrong?

The answer he produced is the matrix on this page. His account of the reasoning is that one parity bit gives one bit of information about the error, so locating an error among nn positions needs about log2n\log_2 n of them, and the question is how to arrange the checks so that each one contributes a full bit. The arrangement that does — each check covering half the positions, and covering them independently — is forced by the counting, which is why the code is essentially unique at each length.

Bell Labs’ patent department held the paper for two years while deciding whether the idea was patentable, which is why Golay’s half-page note of 1949 appeared first. Golay’s paper generalises Hamming’s construction to any prime alphabet and adds the code that carries his name, in a page.

What a burst does to it

One practical limitation belongs here, because it is the reason the next rung exists.

The failure model on this page is isolated errors: at most one bit wrong in seven. Real channels frequently do something else — a scratch on a disc, a dropout on a radio link, a run of cells failing together — and corrupt a burst of consecutive symbols.

A burst of two consecutive bits is exactly the double error the code mishandles. Nothing about the geometry cares that the two flips are neighbours; the arithmetic sees a syndrome and repairs the wrong bit either way.

The standard defence is interleaving: encode many words, then transmit them column by column so that consecutive transmitted bits belong to different codewords. A burst of length bb then contributes at most one error to each of bb codewords, and each is repaired independently. That is a genuinely cheap fix and it costs only latency, since a whole block has to be collected before anything can be decoded.

The other defence is to change the alphabet so that a burst of bits becomes a single wrong symbol, which is what the next rung does. A symbol drawn from a field of 256 elements is a byte, and a burst of eight bad bits inside one byte is one error rather than eight — which is why the codes protecting discs and deep-space links work over bytes and not over bits.

Where this ladder goes

Everything so far has been about bits and flips. The last rung of this ladder changes both.

The alphabet becomes a whole finite field rather than two symbols, a codeword becomes the list of values of a polynomial, and the errors become erasures — symbols known to be missing rather than silently wrong. That change of model turns the correction argument into something much simpler and much stronger: a polynomial of degree under kk is determined by any kk of its values, so any nkn-k symbols may be lost, and it does not matter which ones.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Named objects

A dashed tag is an object no other essay names yet.

CosetDecision procedureError correcting codeHamming codeLinearityParityParity checkSyndrome