Error-Correcting Codes: Introduction

How can a message survive noise without simply being repeated three times over? This is the concrete question behind error-correcting codes. We want to add just enough redundancy to a message. This lets a receiver detect, and even correct, corruption introduced by a noisy channel. At the same time, we want to waste as little space as possible. The subject sits at a remarkable crossroads of Boolean Algebras, linear algebra over finite fields, and combinatorial design. It is one of the few areas of pure discrete mathematics with an immediate, load-bearing engineering payoff. Every compact disc, deep-space probe, and QR code owes its reliability to these ideas. The central definitional move is to measure "how different" two strings of symbols are using the Hamming distance. We then ask how far apart we can force the codewords of a code to be. At the same time, we need enough of them to encode all our messages. This turns error correction into a question about packing points in a discrete metric space. It is a genuinely combinatorial problem, not just an engineering trick. The parameters of a code (length, dimension, minimum distance) become the object of study. This is exactly as in Finite Projective Planes, where one studies parameters (points, lines, incidences) of a combinatorial configuration. The story culminates, at this introductory level, in Hamming's 1950 code. It is a beautifully economical binary code that corrects any single error. It does so using only a logarithmic amount of redundancy. Its construction reveals a deep and recurring theme in combinatorics. An efficient error-correcting code is, in disguise, a system of incidence relations. These relations are dense enough to pin down error locations uniquely. This is exactly the spirit of the finite geometric structures you have already met. This article gives the precise definitions of Hamming distance and the parameters [n,k,d][n,k,d] of a code. It proves the Singleton-type relationship between these parameters and error-correction capability. It also constructs the Hamming code in full detail. It proves both that it corrects one error and that it is perfect — it wastes nothing.

Key takeaways
  • The Hamming distance between two strings counts the positions in which they differ; it is a genuine metric on the space of length-nn strings.
  • A code's minimum distance dd controls its power: it can detect up to d−1d-1 errors and correct up to ⌊(d−1)/2⌋\lfloor (d-1)/2 \rfloor errors.
  • Binary linear codes are described by three parameters [n,k,d][n,k,d]: length, dimension (message size), minimum distance.
  • The Hamming(7,4)(7,4) code uses 3 parity checks to correct any single bit-flip in a 7-bit block carrying 4 message bits.
  • The Hamming code is perfect: every possible received word lies within distance 1 of exactly one codeword, so no redundancy is wasted.
  • Decoding reduces to reading a syndrome, a 3-bit number that literally spells out the binary address of the error.
  • Key idea: spreading redundancy across positions via parity checks turns error location into solving a small linear system over F2\mathbb{F}_2.

Where this lives today

Error-correcting codes underlie the physical layer of essentially all digital storage and communication. Compact discs and DVDs use Reed–Solomon codes (a generalization of the ideas here to larger alphabets) to survive scratches; deep-space probes such as Voyager used convolutional and Reed–Solomon codes to recover images sent across billions of kilometers with a vanishingly weak signal. Computer memory (ECC RAM) uses extended Hamming codes (adding one more overall parity bit to also detect, though not correct, double errors) to guard against cosmic-ray-induced bit flips. QR codes use Reed–Solomon coding to remain scannable even when partially obscured or damaged. In cryptography, the McEliece cryptosystem builds public-key encryption directly on the hardness of decoding a general linear code, using the Hamming-type structure as its trapdoor. In statistics and combinatorial design, the sphere-packing bound proved above is a special case of general packing bounds studied alongside Finite Projective Planes, since perfect codes and projective/Steiner-type designs share the same "exact covering" combinatorial signature — indeed the columns of the parity-check matrix HH above, viewed as the seven nonzero points of the Fano plane PG(2,2)\mathrm{PG}(2,2), is not a coincidence: the Hamming code's incidence structure literally is the Fano plane's point set indexing.

:::quiz[For the [7,4,3][7,4,3] Hamming code, if the syndrome computed from a received word is (0,1,1)(0,1,1) (reading rows top to bottom), which position was in error?]

  • Position 2
  • Position 3 ✓
  • Position 6
  • No error occurred

:::quiz[What makes the [7,4,3][7,4,3] Hamming code "perfect"?]

  • It has the largest possible dimension for its length
  • The radius-1 balls around codewords exactly partition the ambient space ✓
  • It can correct any 2-bit error
  • Its generator matrix is the identity matrix

Ask the tutor: How does the Hamming code's construction generalize to correct two or more errors, as in BCH and Reed–Solomon codes?

DefinitionHamming distance, codes, and parameters

Let Σ\Sigma be a finite alphabet (for the classical case, Σ=F2={0,1}\Sigma = \mathbb{F}_2 = \{0,1\}) and fix a length n≥1n \geq 1. For x=(x1,…,xn),y=(y1,…,yn)∈Σnx = (x_1,\dots,x_n), y=(y_1,\dots,y_n) \in \Sigma^n, the Hamming distance is

dH(x,y)  =  ∣{ i∈{1,…,n}:xi≠yi }∣.d_H(x,y) \;=\; \big|\{\, i \in \{1,\dots,n\} : x_i \neq y_i \,\}\big|.

The Hamming weight of xx is wt(x)=dH(x,0)\mathrm{wt}(x) = d_H(x,\mathbf{0}), the number of nonzero coordinates. A code CC is a nonempty subset C⊆ΣnC \subseteq \Sigma^n; its elements are called codewords. The minimum distance of CC is

d(C)  =  min⁡x,y∈Cx≠ydH(x,y),d(C) \;=\; \min_{\substack{x,y \in C \\ x \neq y}} d_H(x,y),

defined when ∣C∣≥2|C| \geq 2 (by convention d(C)=∞d(C) = \infty, or undefined, if ∣C∣=1|C|=1). If Σ=F2\Sigma = \mathbb{F}_2 and CC is a linear subspace of F2n\mathbb{F}_2^n of dimension kk, we call CC a linear code with parameters [n,k,d][n,k,d], where d=d(C)d = d(C). Such a code is specified by a generator matrix GG, a k×nk \times n matrix over F2\mathbb{F}_2 whose row space is CC, encoding a message m∈F2km \in \mathbb{F}_2^k as the codeword mGmG; equivalently it may be specified by a parity-check matrix HH, an (n−k)×n(n-k)\times n matrix with

C={x∈F2n:Hx⊤=0}.C = \{ x \in \mathbb{F}_2^n : Hx^\top = 0 \}.

Intuition

Think of Σn\Sigma^n as the vertex set of the nn-dimensional Boolean hypercube, where two vertices are joined by an edge exactly when they differ in one coordinate — precisely the object you already know from Boolean Algebras as the free Boolean lattice's covering graph. Hamming distance is graph distance in this cube. A code is then just a chosen set of vertices, and the code's power comes from how far apart, in graph distance, its chosen vertices are forced to be. If we place a "ball" of radius tt around each codeword — all strings within Hamming distance tt — then correcting up to tt errors is possible exactly when these balls are pairwise disjoint, so that any received (possibly corrupted) word lands in the ball of at most one codeword. This is a packing condition, the same combinatorial flavor of question that governs point-line configurations in Finite Projective Planes: how many objects can be placed so that pairwise "closeness" constraints are respected. When the balls not only stay disjoint but also exhaust the whole space, no vertex is wasted — this is the perfection condition that the Hamming code will satisfy exactly.

Figure
The cube {0,1}3\{0,1\}^3: two codewords at Hamming distance 3, with radius-1 balls around each shown shaded
000 111 001 011 010 110 101 Codewords 000, 111 at distance 3 radius-1 balls (shaded) are disjoint ⇒ 1 error correctable

Order, Lattices & Design context

The subspace lattice of F2n\mathbb{F}_2^n, ordered by inclusion, is a finite modular lattice; linear codes are simply elements of this lattice singled out by an extra metric requirement (a lower bound on pairwise Hamming distance among nonzero vectors' differences). This is the same "combinatorial design under order constraints" theme you saw with incidence structures: an incidence system becomes a projective plane once axioms of order and regularity are imposed, and a subspace becomes an error-correcting code once a distance axiom is imposed.

TheoremError detection and correction from minimum distance

Let C⊆ΣnC \subseteq \Sigma^n be a code with minimum distance d=d(C)d = d(C), and let t=⌊(d−1)/2⌋t = \lfloor (d-1)/2 \rfloor. Then: 1. (Detection) If a codeword c∈Cc \in C is transmitted and at most d−1d-1 symbol errors occur, the resulting received word rr is not a codeword (so the error is detected), unless r=cr = c. 2. (Correction) If a codeword c∈Cc \in C is transmitted and at most tt symbol errors occur, then cc is the unique codeword within Hamming distance tt of the received word rr; hence nearest-codeword decoding correctly recovers cc. 3. These bounds are tight: there exist a code CC with minimum distance dd, a codeword cc, and an error pattern of weight dd that transmits cc to another codeword (defeating detection), and an error pattern of weight t+1t+1 that puts rr equidistant from two codewords (defeating correction).

Proof

We prove each part in turn. Throughout, "at most ee errors" transforms cc into rr with dH(c,r)≤ed_H(c,r) \le e.

(1) Detection. Suppose c∈Cc \in C is sent, rr is received with 1≤dH(c,r)≤d−11 \le d_H(c,r) \le d-1, and suppose toward a contradiction that r∈Cr \in C. Since r≠cr \ne c (as dH(c,r)≥1d_H(c,r)\ge 1) and both lie in CC, the definition of minimum distance gives dH(c,r)≥dd_H(c,r) \ge d. This contradicts dH(c,r)≤d−1d_H(c,r) \le d - 1. Hence r∉Cr \notin C, i.e. the corruption is detected because the received word fails to be a codeword.

(2) Correction. Suppose c∈Cc\in C is sent and rr is received with dH(c,r)≤td_H(c,r) \le t. We must show that if c′∈Cc' \in C with c′≠cc' \ne c, then dH(c′,r)>td_H(c', r) > t, so that cc is the unique nearest codeword. By the triangle inequality for the Hamming metric (verified below), and minimality of dd,

dH(c,c′)  ≤  dH(c,r)+dH(r,c′).d_H(c,c') \;\le\; d_H(c,r) + d_H(r,c').

Since c≠c′c \ne c' are both in CC, dH(c,c′)≥dd_H(c,c') \ge d. Combining,

d  ≤  dH(c,c′)  ≤  dH(c,r)+dH(r,c′)  ≤  t+dH(r,c′),d \;\le\; d_H(c,c') \;\le\; d_H(c,r) + d_H(r,c') \;\le\; t + d_H(r,c'),

so dH(r,c′)≥d−td_H(r,c') \ge d - t. Now by definition t=⌊(d−1)/2⌋t = \lfloor (d-1)/2\rfloor, so 2t≤d−12t \le d-1, i.e. d−t≥t+1d - t \ge t+1. Hence dH(r,c′)≥t+1>t≥dH(c,r)d_H(r,c') \ge t+1 > t \ge d_H(c,r). Thus cc is strictly closer to rr than any other codeword, proving uniqueness of the nearest codeword and that it equals cc. (Triangle inequality for dHd_H, used above.) For x,y,z∈Σnx,y,z \in \Sigma^n, if xi≠zix_i \ne z_i then xi≠yix_i \ne y_i or yi≠ziy_i \ne z_i (else xi=yi=zix_i = y_i = z_i, a contradiction), so each coordinate contributing to dH(x,z)d_H(x,z) contributes to dH(x,y)d_H(x,y) or dH(y,z)d_H(y,z); summing over coordinates gives dH(x,z)≤dH(x,y)+dH(y,z)d_H(x,z) \le d_H(x,y)+d_H(y,z). Symmetry dH(x,y)=dH(y,x)d_H(x,y)=d_H(y,x) and dH(x,y)=0  ⟺  x=yd_H(x,y)=0 \iff x=y are immediate from the definition, so dHd_H is indeed a metric.

(3) Tightness. Take any CC with two codewords c,c′c, c' at the minimum distance dd (such a pair exists by definition of dd as a minimum over a nonempty finite set of pairs). Change exactly dd coordinates of cc, precisely the coordinates where cc and c′c' differ, obtaining r=c′r = c'; this is a weight-dd error pattern taking cc to the codeword c′c', defeating detection, proving tightness of (1). For correction, again let c,c′c,c' realize dH(c,c′)=dd_H(c,c')=d, and let SS be the set of the dd coordinates where they differ. Since t=⌊(d−1)/2⌋t = \lfloor(d-1)/2\rfloor, we have 2t+1≤d2t+1 \le d (as 2t≤d−12t \le d-1) hence 2(t+1)≤d+12(t+1) \le d+1; choose a subset S1⊆SS_1 \subseteq S of size t+1t+1 (possible since ∣S∣=d≥t+1|S| = d \ge t+1, because d≥2t+1>td \ge 2t+1 > t) and let rr be cc modified on exactly the coordinates in S1S_1 (flipped to agree with c′c' there, in the binary case; in general changed to any distinct symbol). Then dH(c,r)=t+1d_H(c,r) = t+1. One checks dH(c′,r)=d−(t+1)d_H(c',r) = d - (t+1) in the binary alphabet case since rr agrees with c′c' exactly on S1S_1 within SS and agrees with cc (hence disagrees with c′c', since c,c′c,c' differ everywhere on SS) outside S1S_1 within SS, and r=cr=c outside SS so rr disagrees with c′c' there exactly where cc does. When d=2t+1d = 2t+1 (the case realized by the Hamming code below, where d=3,t=1d=3,t=1), d−(t+1)=td-(t+1) = t, so dH(c′,r)=t≤t+1=dH(c,r)d_H(c',r)=t \le t+1 = d_H(c,r), showing rr is at least as close to c′c' as declared bound to cc, breaking the uniqueness guarantee for t+1t+1 errors. This produces the required example of correction failing once error weight exceeds tt. ■\blacksquare

∎

Worked examples

Example 1: A simple triple-repetition code and its limits. Let C={000,111}⊂F23C = \{000, 111\} \subset \mathbb{F}_2^3, a linear code with parameters [3,1,3][3,1,3] (length 3, one message bit, and minimum distance d=dH(000,111)=3d = d_H(000,111) = 3). By the theorem, t=⌊(3−1)/2⌋=1t = \lfloor (3-1)/2 \rfloor = 1, so this code corrects any single bit-flip. Suppose the message bit is 11, encoded as c=111c = 111, and the channel flips the second bit, producing r=101r = 101. We check dH(r,000)=2d_H(r, 000) = 2 and dH(r,111)=1d_H(r, 111) = 1. Since 1<21 < 2, nearest-codeword decoding correctly outputs 111111, hence message bit 11. This matches the theorem's guarantee since only 1≤t=11 \le t=1 error occurred. Now suppose two bits flip: c=111→r=001c = 111 \to r = 001. Then dH(r,000)=1d_H(r,000) = 1 and dH(r,111)=2d_H(r,111) = 2, so nearest-codeword decoding outputs 000000 — the wrong codeword. This is exactly the tightness phenomenon from part (3) of the theorem: with 2=t+12 = t+1 errors, decoding can fail. Note the huge cost of this code: three symbols of channel bandwidth buy only one bit of message, a rate of 1/31/3, to correct a single error. The Hamming code below will do far better.

Example 2: Constructing and decoding the Hamming [7,4,3][7,4,3] code. We now build the celebrated Hamming code carrying k=4k=4 message bits in n=7n=7 transmitted bits, correcting any single error, using n−k=3n-k = 3 parity checks — logarithmically many, since 23−1=72^3 - 1 = 7. Construction. Index the seven coordinate positions 1,…,71,\dots,7 by the nonzero vectors of F23\mathbb{F}_2^3, written as their binary representations:

1→001,2→010,3→011,4→100,5→101,6→110,7→111.1 \to 001,\quad 2\to 010,\quad 3\to 011,\quad 4\to 100,\quad 5\to 101,\quad 6\to 110,\quad 7\to 111.

Let HH be the 3×73\times 7 parity-check matrix whose ii-th column is the binary representation of ii:

H  =  (000111101100111010101).H \;=\; \begin{pmatrix} 0&0&0&1&1&1&1\\ 0&1&1&0&0&1&1\\ 1&0&1&0&1&0&1 \end{pmatrix}.

The code is C={x∈F27:Hx⊤=0}C = \{x \in \mathbb{F}_2^7 : Hx^\top = 0\}, a linear subspace of dimension k=7−rank(H)=7−3=4k = 7 - \mathrm{rank}(H) = 7-3=4 (the rank is 33 because the seven columns include the three standard basis vectors 100,010,001100,010,001, forcing full row rank). Take an explicit, asymmetric message. Let the message be m=(1,0,1,1)∈F24m = (1,0,1,1) \in \mathbb{F}_2^4, placed by convention in the "data" positions 3,5,6,73,5,6,7 (the positions whose index is not a power of 22), with positions 1,2,41,2,4 reserved as parity bits, chosen so that Hx⊤=0Hx^\top=0. Concretely, position 11 (binary 001001) checks the parity of all positions whose binary index has a 11 in the last bit: positions 1,3,5,71,3,5,7. Position 22 (010010) checks positions 2,3,6,72,3,6,7. Position 44 (100100) checks positions 4,5,6,74,5,6,7. With data bits x3=1,x5=0,x6=1,x7=1x_3=1,x_5=0,x_6=1,x_7=1 (matching m=(1,0,1,1)m=(1,0,1,1) in order), we solve:

x1=x3⊕x5⊕x7=1⊕0⊕1=0,x_1 = x_3\oplus x_5 \oplus x_7 = 1\oplus 0 \oplus 1 = 0,
x2=x3⊕x6⊕x7=1⊕1⊕1=1,x_2 = x_3 \oplus x_6 \oplus x_7 = 1 \oplus 1 \oplus 1 = 1,
x4=x5⊕x6⊕x7=0⊕1⊕1=0.x_4 = x_5\oplus x_6\oplus x_7 = 0 \oplus 1 \oplus 1 = 0.

So the transmitted codeword is

c=(x1,…,x7)=(0,1,1,0,0,1,1).c = (x_1,\dots,x_7) = (0,1,1,0,0,1,1).

Verify Hc⊤=0Hc^\top = 0: row 1 sums positions 1,3,5,71,3,5,7: 0+1+0+1=2≡00+1+0+1 = 2 \equiv 0. Row 2 sums positions 2,3,6,72,3,6,7: 1+1+1+1=4≡01+1+1+1=4\equiv 0. Row 3 sums positions 4,5,6,74,5,6,7: 0+0+1+1=2≡00+0+1+1=2\equiv 0. All three checks vanish, confirming c∈Cc \in C. Introduce a single error. Suppose the channel flips position 55: received word is

r=(0,1,1,0,1,1,1).r = (0,1,1,0,\mathbf{1},1,1).

Decode via the syndrome. Compute s=Hr⊤∈F23s = Hr^\top \in \mathbb{F}_2^3: Row 1 (positions 1,3,5,71,3,5,7): 0+1+1+1=3≡10+1+1+1 = 3 \equiv 1. Row 2 (positions 2,3,6,72,3,6,7): 1+1+1+1=4≡01+1+1+1 = 4 \equiv 0. Row 3 (positions 4,5,6,74,5,6,7): 0+1+1+1=3≡10+1+1+1 = 3 \equiv 1. So s=(1,0,1)s = (1,0,1). Read as a binary number this is 1012=5101_2 = 5 — exactly the position that was flipped! This is not a coincidence but the design principle: HH's ii-th column is the binary representation of ii, so if a single error occurs at position jj, then r=c+ejr = c + e_j (where eje_j is the standard basis vector), and

Hr⊤=Hc⊤+Hej⊤=0+(column j of H)=binary(j),Hr^\top = Hc^\top + He_j^\top = 0 + (\text{column } j \text{ of } H) = \text{binary}(j),

since Hc⊤=0Hc^\top = 0 because c∈Cc \in C. Flipping the bit the syndrome names, position 55, recovers exactly c=(0,1,1,0,0,1,1)c = (0,1,1,0,0,1,1), and stripping the parity positions 1,2,41,2,4 recovers the message m=(1,0,1,1)m = (1,0,1,1) correctly.

Detailed derivation of parameters

We now prove the minimum distance d=3d=3 carefully rather than asserting it, and prove perfection.

LemmaMinimum distance of the Hamming code is 3

For the code C⊂F27C \subset \mathbb{F}_2^7 defined above, d(C)=3d(C) = 3.

Proof

For a linear code, d(C)=min⁡0≠x∈Cwt(x)d(C) = \min_{0 \ne x \in C} \mathrm{wt}(x), because dH(x,y)=wt(x−y)=wt(x+y)d_H(x,y) = \mathrm{wt}(x-y) = \mathrm{wt}(x+y) over F2\mathbb{F}_2 (linearity: x−y∈Cx - y \in C whenever x,y∈Cx,y \in C, and this is a bijection between pairs and nonzero codewords), and the minimum over all pairs equals the minimum weight over nonzero elements. We use this reduction, proved once and applied throughout, standard for linear codes. First, d(C)≥3d(C) \ge 3: suppose x∈Cx \in C is nonzero with wt(x)≤2\mathrm{wt}(x) \le 2. If wt(x)=1\mathrm{wt}(x)=1, say x=ejx = e_j, then Hx⊤Hx^\top equals column jj of HH, which is nonzero (it is the binary representation of j≥1j \ge 1), contradicting x∈Cx \in C. If wt(x)=2\mathrm{wt}(x) = 2, say x=ei+ejx = e_i + e_j with i≠ji \ne j, then Hx⊤=Hx^\top = column ii ++ column jj (mod 2); since all seven columns are the distinct nonzero vectors of F23\mathbb{F}_2^3, and i≠ji\ne j means these are distinct nonzero binary triples, their sum is nonzero (two distinct nonzero vectors in F23\mathbb{F}_2^3 cannot sum to zero, since that would require them to be equal). So Hx⊤≠0Hx^\top \ne 0, again contradicting x∈Cx \in C. Hence no nonzero codeword has weight 11 or 22, so d(C)≥3d(C) \ge 3. Second, d(C)≤3d(C) \le 3: exhibit a weight-3 codeword. Take columns indexed 1,2,31,2,3 of HH, namely 001,010,011001, 010, 011; their sum is 001+010+011=000001+010+011 = 000 (since 0⊕0⊕0=00\oplus0\oplus0=0 in each coordinate: first coordinates 0,0,00,0,0; second 0,1,10,1,1; third 1,0,11,0,1 — sum coordinatewise mod 2 gives (0,0,0)(0,0,0) ). Hence x=e1+e2+e3x = e_1+e_2+e_3 satisfies Hx⊤=0Hx^\top = 0, so x∈Cx \in C has weight 33. Combining both bounds, d(C)=3d(C) = 3. ■\blacksquare

∎
CorollaryThe Hamming code corrects exactly one error

With d=3d=3, t=⌊(3−1)/2⌋=1t = \lfloor (3-1)/2\rfloor = 1. By Error detection and correction from minimum distance, CC corrects any single-bit error and detects (but does not always correct) any double-bit error.

TheoremPerfection of the Hamming code

The code C⊂F27C \subset \mathbb{F}_2^7 above is a perfect code: the closed Hamming balls of radius 11 around the 24=162^4=16 codewords partition all of F27\mathbb{F}_2^7.

Proof

We use the sphere-packing bound, first establishing it in general and then checking equality here — this is a counting argument, not an assumption. For q=2q=2 (binary alphabet), the closed ball of radius tt around any fixed vector in F2n\mathbb{F}_2^n has size

V(n,t)=∑i=0t(ni),V(n,t) = \sum_{i=0}^t \binom{n}{i},

since a vector at distance exactly ii from the center is obtained by flipping any subset of ii of the nn coordinates, and there are (ni)\binom{n}{i} such subsets, for each 0≤i≤t0\le i\le t. Disjointness. By part (2) of Error detection and correction from minimum distance applied with t=1t=1 (valid since d=3≥2t+1=3d=3 \ge 2t+1=3), for any two distinct codewords c,c′c,c' the balls of radius 11 around them are disjoint: if some rr were in both, then rr would be within distance 11 of two distinct codewords, contradicting the uniqueness of the nearest codeword proved in that theorem (with t=1t=1; indeed uniqueness there shows that whenever dH(c,r)≤td_H(c,r)\le t we get dH(c′,r)>td_H(c',r) > t for c′≠cc'\ne c, ruling out rr being simultaneously within distance tt of both). Counting. Since the 1616 balls of radius 11 are pairwise disjoint subsets of F27\mathbb{F}_2^7, each of size V(7,1)=(70)+(71)=1+7=8V(7,1) = \binom{7}{0}+\binom{7}{1} = 1+7=8, their union has size 16×8=12816 \times 8 = 128. But ∣F27∣=27=128|\mathbb{F}_2^7| = 2^7 = 128 exactly. Since the union of disjoint balls has size equal to the size of the whole space, the balls must cover the whole space (their union, of size 128128, is a subset of the 128128-element space, and a subset of equal cardinality to a finite set must be the whole set). Hence the balls partition F27\mathbb{F}_2^7: they are pairwise disjoint and their union is everything. This proves perfection: every one of the 128128 possible received words lies in the radius-1 ball of exactly one codeword, so single-error correction is always achievable and no capacity is wasted (equality holds in the sphere-packing/Hamming bound ∣C∣⋅V(n,t)≤2n|C| \cdot V(n,t) \le 2^n, which in general reads 16×8≤12816\times 8 \le 128 and here is equality). ■\blacksquare

∎
CounterexampleDropping linearity: minimum distance can silently fail to control worst-case correction, and "distance" computations become pairwise, not weight-based

The theorem on detection/correction assumed only that CC is a code with a well-defined minimum distance d(C)d(C); that part needs no linearity. But the practical simplification used above — that for linear codes d(C)d(C) equals the minimum nonzero codeword weight, so one need not check all (∣C∣2)\binom{|C|}{2} pairs — genuinely requires linearity, and dropping it breaks the shortcut. Let Σ=F2\Sigma = \mathbb{F}_2, n=3n=3, and take the non-linear code

C={000, 011, 101}.C = \{000,\ 011,\ 101\}.

This CC is not closed under addition: 011+101=110∉C011 + 101 = 110 \notin C, so it is not a linear subspace. Compute pairwise distances directly: dH(000,011)=2d_H(000,011)=2, dH(000,101)=2d_H(000,101)=2, dH(011,101)=2d_H(011,101)=2. So d(C)=2d(C)=2. If we incorrectly imported the linear shortcut "d(C)=d(C) = minimum nonzero weight," we would compute min⁡(wt(011),wt(101))=min⁡(2,2)=2\min(\mathrm{wt}(011),\mathrm{wt}(101)) = \min(2,2) = 2 — which happens to agree here, so let's push further to see the shortcut fail outright. Extend to

C′={000, 011, 101, 111}.C' = \{000,\ 011,\ 101,\ 111\}.

Now wt(000)=0\mathrm{wt}(000)=0 is not counted (it's the zero vector, excluded as "nonzero weight" would require), and among nonzero words the minimum weight is wt(011)=wt(101)=2\mathrm{wt}(011)=\mathrm{wt}(101)=2, so the naive shortcut predicts d(C′)=2d(C') = 2. But compute the true pairwise minimum: dH(011,111)=1d_H(011,111) = 1 (they differ only in the first coordinate). So the actual minimum distance is d(C′)=1d(C') = 1, not 22: the naive linear shortcut is simply wrong here because C′C' is not linear (011+101=110∉C′011+101 = 110 \notin C', so closure fails and "distance to zero" no longer represents all pairwise gaps). The consequence for the main theorem is severe: with true d(C′)=1d(C')=1, we get t=⌊(1−1)/2⌋=0t = \lfloor (1-1)/2\rfloor = 0 — this code corrects zero errors, even though the flawed weight-based shortcut suggested d=2d=2 and single-error detection. Trusting the shortcut without linearity would have caused us to certify error-correction guarantees the code does not have. This shows precisely why the reduction "minimum distance = minimum nonzero weight" is stated only for linear codes, and why that hypothesis cannot be dropped.

Scope, edge cases & common traps

SituationWhat holdsWhat fails / must be checked
CC linear over F2n\mathbb{F}_2^nd(C)=d(C) = min weight of nonzero codewordRequires closure under ++; false for general (non-linear) codes
d=2t+1d = 2t+1 (odd)Corrects exactly tt errors, sphere bound may be tightPerfection is rare; only a few parameter families achieve it
d=2td = 2t (even)Corrects t−1t-1 errors, detects ttEasy to mis-state correction radius by one; always recompute ⌊(d−1)/2⌋\lfloor (d-1)/2\rfloor
Non-binary alphabet q>2q>2Sphere-packing bound generalizes with (ni)(q−1)i\binom{n}{i}(q-1)^iColumn/index bijection trick for Hamming-type codes needs qq-ary generalization
Received word outside all balls of radius ttDecoder can detect failure ("decode error")Must not silently output nearest codeword if ties or large distance occur

The mathematician behind it

Origins · 1 minRichard Wesley Hamming and how this was discovered
RW
Richard Wesley Hamming
1915–1998 · Bell Telephone Laboratories, mid-20th-century USA

Hamming worked on early relay-based computers at Bell Labs in the late 1940s, running long unattended batch computations over weekends. Machines of the era would halt on the first detected error rather than correct it, silently discarding an entire weekend's computation. Frustrated by this, Hamming asked a sharper combinatorial question: rather than merely detecting an error, could redundant bits be arranged so that the location of a single error could be pinpointed and fixed automatically? His 1950 paper answered yes, giving the [7,4,3][7,4,3] Hamming code and the general family [2r−1,2r−1−r,3][2^r-1, 2^r-1-r, 3], together with the sphere-packing perfection argument reproduced above.

History
- 1947–1948: Hamming, frustrated by unattended relay computer runs failing on undetected/uncorrected errors, begins designing self-correcting schemes at Bell Labs.
- 1948: Claude Shannon's "A Mathematical Theory of Communication" establishes the theoretical possibility of reliable communication over noisy channels, providing the surrounding information-theoretic context.
- 1950: Hamming publishes "Error Detecting and Error Correcting Codes," introducing Hamming distance and the [7,4,3][7,4,3] Hamming code.
- 1954: David Muller and Irving Reed introduce Reed–Muller codes, extending the design idea to correct multiple errors.
- 1960: Irving Reed and Gustave Solomon introduce Reed–Solomon codes over larger finite fields, later foundational for CDs, DVDs, and QR codes.
Why is Hamming distance a metric, and does that matter practically?

Hamming distance satisfies non-negativity, identity of indiscernibles, symmetry, and the triangle inequality, all verified directly from the definition (the triangle inequality proof is given explicitly in the main theorem's proof). This matters practically because decoding algorithms rely on the triangle inequality to guarantee that "nearest codeword" decoding behaves consistently — without it, statements like "if cc is close to rr and rr is close to no other codeword, then cc is the unique closest codeword" would need separate justification for every code rather than following once from general metric-space reasoning.

Is the Hamming code the only perfect binary code?

No, but perfect codes are rare. Besides the Hamming codes [2r−1,2r−1−r,3][2^r-1, 2^r-1-r, 3] for each r≥2r \ge 2 and their trivial cousins (the whole space, a single repetition code of odd length, and single codewords), the only other nontrivial perfect binary linear code is the [23,12,7][23,12,7] binary Golay code, which corrects up to 3 errors. A deep theorem (van Lint, Tietäväinen, early 1970s) classifies all perfect codes over prime-power alphabets and shows this list is essentially exhaustive, a result well beyond this introductory scope.

Does a larger minimum distance always mean a "better" code?

Not for free — there is a trade-off captured by bounds such as the Singleton bound (d≤n−k+1d \le n-k+1) and the sphere-packing bound used above. Increasing dd while holding nn fixed generally forces kk (and hence the number of messages 2k2^k) to shrink, since codewords must be pushed farther apart in a fixed-size space. Good codes are precisely those achieving a favorable trade-off among nn, kk, and dd simultaneously, which is why code parameters [n,k,d][n,k,d] are always quoted together rather than optimizing dd in isolation.

What happens if two errors occur in the Hamming code — does the syndrome just give a wrong location?

Yes, and this is instructive. With two errors at positions i≠ji \ne j, the syndrome becomes binary(i)⊕binary(j)\mathrm{binary}(i) \oplus \mathrm{binary}(j) (mod 2, coordinatewise), which equals binary(k)\mathrm{binary}(k) for some third position k≠i,jk \ne i,j (since the nonzero vectors of F23\mathbb{F}_2^3 form a group under XOR with no nonzero element being its own inverse besides itself, forcing this XOR to land on a third distinct nonzero vector whenever i≠ji\ne j). The decoder will "correct" position kk, which was actually fine, introducing a third error and moving even further from the true codeword. This is exactly the failure mode predicted abstractly by the tightness part of the detection/correction theorem.

How does the Hamming code relate to the Fano plane and finite projective planes?

The seven nonzero vectors of F23\mathbb{F}_2^3, used as column labels for the parity-check matrix, are exactly the seven points of the smallest projective plane, PG(2,2)\mathrm{PG}(2,2), the Fano plane, when its lines are read off as the triples of points summing to zero. The weight-3 codewords of the Hamming code (there are exactly 7 of them, up to complementation considerations) correspond precisely to the lines of the Fano plane. This is the first small instance of a much larger correspondence between linear codes and combinatorial designs, in which projective and affine planes generate especially symmetric codes.

Can these ideas be extended beyond single-error correction?

Yes; this article covers the introductory single-error-correcting case, but the same framework of Hamming distance, minimum distance dd, and the correction radius t=⌊(d−1)/2⌋t=\lfloor (d-1)/2\rfloor extends directly to codes correcting many errors, such as BCH codes, Reed–Solomon codes, and Golay codes, all built from richer algebraic structures (finite fields and polynomial rings) layered on top of the same linear-algebraic and metric foundations introduced here.

Why does the Hamming bound (sphere-packing bound) hold in general, not just for this example?

The argument used above — that disjoint balls around codewords cannot collectively exceed the size of the ambient space — uses only that Hamming balls of radius tt around distinct codewords are disjoint whenever d≥2t+1d \ge 2t+1, a consequence purely of the triangle inequality and the definition of minimum distance, independent of any specific code. This gives the general inequality ∣C∣⋅V(n,t)≤qn|C|\cdot V(n,t) \le q^n for any qq-ary code correcting tt errors; the Hamming code is notable precisely because it is one of the few known codes achieving equality.

Before this