Computation

Sixteen spheres that fill a cube

A hundred and twenty-eight seven-bit words, sixteen of them chosen, and a ball of eight around each. Sixteen times eight is a hundred and twenty-eight exactly — so the balls tile the space with nothing left over, and the code wastes nothing at all.

Worth reading first: Distance is a picture.

Counting corners gives a ceiling on how many codewords there is room for. Almost nothing reaches it. One thing does, at length seven, and it does so exactly.

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. 1 The sixteen codewords of the [7,4] Hamming code. Four data bits, three parity bits, and a minimum distance of 3 measured over all 120 pairs. Seven of the sixteen have weight 3, seven have weight 4, and the remaining two are the ends.

The arithmetic that has to work first

A code correcting one error needs the balls of radius one around its codewords to be disjoint. In the nn-cube such a ball holds 1+n1 + n words. So MM codewords need M(1+n)M(1+n) words of room, and there are 2n2^n words altogether:

M2nn+1.M \le \frac{2^n}{n+1}.

For that to be attained the division must come out as a whole number, and — since MM has to be a power of two for a linear code — as a power of two. That happens when n+1n+1 is a power of two, so at n=3,7,15,31,n = 3, 7, 15, 31, \ldots.

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 The bound, computed row by row. 2ⁿ divided by the ball size comes out exactly at n = 3, 7 and 15 and at no other length in range — which is where a perfect code is arithmetically possible and nowhere else.

At n=3n=3 the answer is M=2M = 2, which is the repetition code from the previous essay — perfect, and useless, since two codewords carry one bit.

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. 3 The smallest perfect code, drawn whole. Two codewords, two balls of four, eight corners, nothing left over. Everything that follows is this picture at a size that cannot be drawn.

That the ball has n+1n+1 words in it, and not some more complicated number, is the reason the condition is as clean as it is. A ball of radius one is the centre plus the nn words one flip away, and the binomial coefficients that appear at larger radii have not started yet.

At n=7n=7 the answer is M=16M = 16, which carries four bits in seven. That is a rate of 4/74/7, a genuine code rather than a curiosity, and the question is whether sixteen corners of the seven-cube can actually be found sitting at distance three from one another.

The construction

They can, and the construction is four data bits with three parity bits computed from them.

Let the data be d1d2d3d4d_1 d_2 d_3 d_4 and set

p1=d1+d2+d3,p2=d2+d3+d4,p3=d1+d2+d4,p_1 = d_1 + d_2 + d_3,\qquad p_2 = d_2 + d_3 + d_4,\qquad p_3 = d_1 + d_2 + d_4,

all sums taken modulo two. The codeword is d1d2d3d4p1p2p3d_1 d_2 d_3 d_4 p_1 p_2 p_3.

Sixteen data patterns give sixteen codewords, listed in the figure at the top of this page with their weights. The choice of which data bits each parity bit watches is a choice, and it is the only choice in the whole construction; what makes it the right one is checked below rather than argued for.

Why the choice of parity checks is the whole design

Three parity bits watching four data bits could be arranged in many ways, and most arrangements give a worse code. It is worth seeing what separates the good arrangement from the bad ones, because the answer is a single sentence and it is the reason the next essay works.

Write the three equations as a matrix over the two-element field, with one column per position, and read off the columns. Position 11 enters checks 11 and 33, so its column is 101101. Position 22 enters all three, giving 111111. Position 55 is the first parity bit and enters only its own check, giving 100100. Running through all seven positions produces the seven columns

101,111,110,011,100,010,001,101,\quad 111,\quad 110,\quad 011,\quad 100,\quad 010,\quad 001,

which are the seven non-zero three-bit patterns, each appearing exactly once.

That is the design condition, and everything follows from it. A code has distance at least three exactly when no two columns are equal and no column is zero — because a codeword of weight one or two would be a column, or a sum of two columns, coming out zero. With three check bits there are 231=72^3 - 1 = 7 non-zero patterns available, so seven is the longest such code, and it uses every pattern.

Length seven is therefore not a lucky number. It is 2312^3-1, and the same argument gives 2r12^r-1 for rr check bits: fifteen with four, thirty-one with five. The perfection and the length come from the same count, which is why the bound is met rather than approached.

Distance three, measured twice

The minimum distance is computed in the figure over all (162)=120\binom{16}{2} = 120 pairs, and it comes out as 33.

It is also computed a second way. The code is linear — the sum of two codewords is a codeword, since the parity equations are linear — and for a linear code the minimum distance equals the minimum weight of a non-zero codeword. That is because the distance between uu and vv is the weight of u+vu+v, which is itself a codeword.

So one hundred and twenty pairwise comparisons and fifteen weight counts are two routes to the same number, and the figure asserts that they agree. Either alone would be a claim about itself.

The weight distribution is worth reading: one word of weight 00, seven of weight 33, seven of weight 44, one of weight 77. It is symmetric, which is not an accident — adding the all-ones word to a codeword gives a codeword, and that pairing swaps weight ww with weight 7w7-w.

The packing, performed

Now the claim this essay is named for.

Around each of the sixteen codewords, the ball of radius one holds 1+7=81 + 7 = 8 words: the codeword and the seven words one flip away. Sixteen balls of eight is one hundred and twenty-eight, which is the number of seven-bit words there are.

That the arithmetic works does not mean the balls do not overlap. So the figure does not compute 16×816 \times 8; it assigns every one of the one hundred and twenty-eight words to a ball and counts.

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. 4 All 128 words filed into 8 classes of 16 by the parity check they fail. Each class has exactly one lightest member, and each lightest member has weight 0 or 1 — which is the packing statement in the language of classes rather than of balls.

The partition is drawn there in its other and slightly better form. The whole space splits into eight classes, one per possible outcome of the three parity checks, each containing sixteen words. Within each class there is exactly one word of least weight, and its weight is at most one.

Those eight lightest words are the zero word and the seven single-bit errors. That is precisely the statement that every word in the space is either a codeword or one flip from exactly one codeword — which is what perfection means, arrived at by partitioning rather than by multiplying.

Perfect, and what it costs

The code is optimal in the strongest available sense: no code of length seven correcting one error can have seventeen codewords, because there is no room, and this one has sixteen.

That optimality has a price, and the price is that there is no slack anywhere. Every word in the space is claimed by some codeword. A word with two errors in it does not look like a word with two errors; it looks like a perfectly ordinary word one flip from a different codeword, and the decoder confidently repairs it into the wrong message.

So a perfect code cannot detect what it cannot correct. The parity code of the previous essay detects one error and corrects none, and is honest about failing. This code corrects one error and is silent about two.

The usual repair is to append one more parity bit over the whole word, giving the extended [8,4][8,4] code with distance 44. It corrects one error and detects two, at the cost of one bit and of perfection — the balls no longer tile, and the words left over are exactly the ones the decoder now refuses rather than mangles.

A code on the 4-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.00000001001000110100010101100111100010011010101111001101111011118 codewords in the 4-cube, minimum distance 2 — no error corrected, 1 detectedthe balls of radius 1 cover 16 of 16 words, 8 of them twice
Fig. 5 What a code with room to spare looks like: distance 2, balls overlapping, and every single error landing outside the code where it can be noticed. Perfection and detection pull in opposite directions.

That trade is a real design decision and not a technicality. A channel where two errors are rare wants the perfect code; a channel where a wrong answer is worse than no answer wants the extended one.

What the design costs to build

It is worth noticing how little machinery the construction needs, because it explains why this code is in hardware everywhere.

Encoding is three exclusive-ors. Checking is three more. Neither operation needs a table, a search, or memory of anything beyond the seven bits in hand. The whole of the arithmetic is in the two-element field, where addition is the exclusive-or gate and multiplication is the and gate — which is why a code is the natural thing for a circuit to compute and a decimal check digit is not.

Memory modules have used the extended [8,4][8,4] version and its larger relatives since the 1970s, under the name SECDED — single error correct, double error detect. A stick of server memory runs a Hamming-style code across every word it stores, corrects the single-bit upsets that cosmic rays and marginal cells produce, and reports the double ones. The design is Hamming’s from 1950, unmodified in any essential way, and the reason it has not been improved on at that size is that at length seven there is provably nothing better.

How many perfect codes there are

The natural next question is which other lengths work, and the answer is one of the more satisfying classification theorems in the subject.

The arithmetic permits perfection at n=3,7,15,31,63,n = 3, 7, 15, 31, 63, \ldots for one error, and at scattered lengths for more. The Hamming construction generalises: for every rr, there is a code of length 2r12^r - 1 with 2r1r2^r - 1 - r data bits correcting one error, perfectly. So the one-error case is settled and the answer is “all of them”.

For more than one error, almost everything fails.

How many words a code of distance 7 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?7642.000yes — 2¹8932.753no91303.938no101765.818no112328.828no1229913.699no1337821.672no1447034.860no1557656.889no1669794.026no17834157.161no18988265.328no191160451.972no201351776.148no2115621342.6no2217942338.0no2320484096.0yes — 2¹²balls of radius 3 hold 64–2048 words, and 2ⁿ divided by that is the mostcodewords distance 7 allowsthe division comes out exactly at n = 7, 23 — the only lengths where a perfectcode is arithmetically possible
Fig. 6 The bound at distance 7, where a ball holds 1 + n + C(n,2) + C(n,3) words. It comes out exactly at n = 7, which is the repetition code, and at n = 23 — where a code really does exist, and it is the only one of its kind.

The one exception is remarkable enough to have its own name. At n=23n = 23, a ball of radius three holds

1+23+253+1771=2048=2111 + 23 + 253 + 1771 = 2048 = 2^{11}

words, and 223/211=2122^{23}/2^{11} = 2^{12}. The arithmetic permits a code with 40964096 codewords of length 2323 correcting three errors, and such a code exists: the binary Golay code, published by Marcel Golay in 1949 in a paper of half a page.

And that is all. A theorem of Tietäväinen and van Lint, completed in 1973, says the perfect binary codes are exactly: the trivial ones, the repetition codes of odd length, the Hamming codes, and the Golay code. Nothing else, at any length, for any number of errors.

Where the arithmetic lies

The classification has a moral, and it is one this collection keeps meeting: a count that comes out right is not an object.

The clean case is length 9090. A ball of radius two in the 9090-cube holds

1+90+4005=4096=2121 + 90 + 4005 = 4096 = 2^{12}

words, and 290/212=2782^{90}/2^{12} = 2^{78} — as exact as any division in this essay. Every necessary condition is met. There is no such code, and the proof that there is not is a genuinely hard argument rather than an oversight.

So the tables in these figures answer a smaller question than they appear to. “Is a perfect code arithmetically possible at this length?” is decided by counting corners, which is what the figures do. “Does one exist?” is a different question with a different and much harder answer, and the two coincide far less often than the length-seven case suggests.

This is exactly the discipline the counting arguments elsewhere in this collection demand. Counting the same thing two ways proves an identity; counting the room something would need proves only that it might fit.

The same gap appears in every subject where a necessary condition is cheap and existence is expensive. Which numbers of points admit a schedule where every pair meets once has a divisibility test that is necessary and — unusually — turns out to be sufficient too, which is the happy case. Which orders admit a pair of orthogonal Latin squares has a necessary condition that Euler believed sufficient and that is not, and the exception took a hundred and seventy years to find. Perfect codes sit at the same crossroads, and length ninety is where the arithmetic’s promise is broken.

There is no general lesson beyond the obvious one, which is that a table of exact divisions is a table of candidates. Nothing in this essay’s figures would look any different if the length-seven code did not exist. What makes it exist is the construction, and what makes the construction correct is the partition the second figure performs.

Two ways of saying the same thing

The packing statement and the class statement are worth holding side by side, because they are the same fact and the second one generalises.

As a packing: the sixteen balls of radius one are disjoint and cover everything.

As a partition: the code is a subgroup of the group of all seven-bit words under bitwise addition, its cosets are the sixteen-word classes in the figure above, and each coset contains exactly one word of weight at most one.

The second version is better for two reasons. It makes the sixteen-ness obvious — every coset of a subgroup has the same size as the subgroup, which is Lagrange’s theorem doing the counting — and it says what decoding is: given a received word, identify its coset, subtract the coset’s lightest member, and the result is a codeword.

That reformulation is what turns a search over sixteen candidates into an arithmetic operation, and it is why the next rung can locate an error without ever listing a codeword. The subgroup structure is available because the code is linear, and the code is linear because parity is addition modulo two and nothing else.

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. 7 The same sixteen words with one of them picked out. Every other codeword differs from it in at least three places, and the seven words differing in exactly one are its own private territory in the packing.

The code that went to Jupiter

One footnote, because the Golay code is not a curiosity.

The Voyager spacecraft used it for their images of Jupiter and Saturn: the [24,12,8][24,12,8] extended Golay code, three errors corrected in every twenty-four bits, at a rate of one half. Later missions moved to the Reed–Solomon codes of the fourth rung of this ladder, which handle bursts better.

There is a pleasing sequence in that. A packing argument on a cube, a half-page paper in 1949, a classification theorem in 1973, and pictures of a planet — all of it turning on the fact that 1+23+253+17711 + 23 + 253 + 1771 happens to be 20482048.

Where this ladder goes

The packing is established, and nothing so far explains how the correction is actually carried out. Finding the nearest of sixteen codewords by comparing against all sixteen is a possible procedure and a poor one.

The next rung shows what the three parity bits are really for. Running them on a received word produces three bits that are zero when nothing is wrong, and otherwise are the binary numeral of the position that broke — the error located directly, without any search and without recovering the message at all.

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.

CosetCounting argumentError correcting codeHamming codeHamming distanceMinimum distancePerfect codeSphere packing bound