Block Codes: Hamming, Extended Hamming & Bounds
Binary linear block codes turn parity equations into a decoder: generator matrices create legal words, parity-check matrices identify error cosets, and minimum distance states exactly what can be detected or corrected. This entry builds and decodes a complete Hamming (7,4) example, extends it to SECDED, and uses classical bounds to judge proposed code parameters.
A block code accepts exactly information symbols, emits exactly channel symbols, and then starts again with no coding memory carried into the next block. The useful question is not merely how many parity bits it adds. It is which words are legal, how far apart those words are, and how a receiver turns a corrupted word into either a correction or an explicit failure.
This entry develops that mechanism for binary linear codes. It assumes the distance and rate concepts introduced in channel-coding basics, then makes them executable with matrices, syndromes, cosets, and a complete Hamming-code decode. Polynomial generators and the algebraic machinery of CRC, BCH, and Reed–Solomon codes belong to cyclic and BCH codes.
The fixed-block contract #
Linearity gives three practical simplifications:
- encoding is a matrix multiplication rather than a table of arbitrary words;
- checking is another matrix multiplication whose zero result characterizes every legal codeword;
- the distance between two codewords equals the weight of their difference, so is the smallest weight of any nonzero codeword.
The last point matters operationally. To establish , a linear-code test examines nonzero codewords, not every pair of codewords. For large that exhaustive search is still impossible, so a real code family needs a proof, a structured distance algorithm, or a conservative bound rather than an unverified claim.
Generator and parity-check matrices #
Let the information row vector be . A full-rank generator matrix encodes it as
A full-rank parity-check matrix describes the same code as a null space:
When the coordinate order is systematic, one convenient form is
Over , subtraction and addition are both XOR, so the minus sign seen in the general-field formula disappears. The first codeword positions then reproduce and the last positions contain parity.
A syndrome identifies an error coset #
Suppose the channel adds an error vector and the receiver gets
The syndrome is
The transmitted codeword vanishes from the calculation. The syndrome depends only on the error's equivalence class: two received words have the same syndrome exactly when their difference is a codeword.
flowchart LR U["message u"] --> E["encode c = uG"] E --> C["channel adds error e"] C --> S["syndrome s = Hrᵀ"] S --> L["choose coset leader ê"] L --> X["correct ĉ = r + ê"]
The possible received words split into disjoint cosets, one for each syndrome. A standard array writes the code itself as the first row, then one row for each translated set . A syndrome decoder stores one chosen coset leader per row and subtracts it—which is the same as adding it in binary.
Why choose the smallest-weight leader? On a binary symmetric channel where independent bit flips are less likely than correct bits, lower-weight error patterns are more probable. That is a maximum-likelihood rule for this channel model, not a theorem that the lowest-weight explanation is always physically right. Bursts, unequal bit reliabilities, and correlated memory faults require a channel-aware metric or an explicit interleaver/error model.
What minimum distance guarantees #
If a code has minimum distance , then it can guarantee
An erasure says which coordinate is unreliable; an error hides both its location and value. Mixed errors and erasures are uniquely correctable when
where is the number of unknown-location errors and is the number of known erasures.
These are worst-case guarantees. A decoder may successfully correct some patterns beyond them, but it must not advertise that luck as code capability. Beyond the guaranteed radius, the same syndrome can have multiple plausible leaders, and a decoder can silently move the received word toward the wrong codeword.
Why the Hamming construction works #
Choose parity checks and form from every distinct nonzero -bit column. There are such columns, giving the binary Hamming family
A one-bit error in position has error vector , so
Every column is nonzero, so no one-bit error has the zero syndrome. Every column is different, so no two positions produce the same syndrome. Equivalently, has no zero column and no pair of dependent columns, which excludes codewords of weight one or two and gives . Some three columns do sum to zero, so .
Hamming's familiar coordinate convention labels positions by their binary numbers and uses those labels as the columns of . Positions hold parity; the remaining positions hold information. Reading the failed parity checks as a binary integer then returns the location of a single error directly.
The construction is perfect for one-error correction. Each codeword owns one no-error word plus possible one-error words, and
The radius-one decoding balls fill the entire binary space without gaps or overlaps. This efficiency also creates the plain Hamming decoder's sharp edge: every nonzero syndrome already names a bit, so there is no unused syndrome left to say “this was a double error.”
Complete Hamming encode and correction #
Use positions for even-parity checks and positions for information. Let syndrome components correspond to parity-check weights :
For message order , a compatible generator is
Take
The information positions are therefore , , , and . The even-parity equations give
Thus
Now flip position 5 in transit:
The receiver computes
Interpreting the components with weights gives . The decoder flips received position 5, recovers , verifies that its new syndrome is zero, and reads the information positions as .
Extended Hamming and SECDED #
Append one bit that makes the parity of the entire Hamming codeword even. Because all weight-three codewords had odd weight, their extensions gain one more nonzero coordinate; the result is
For , Hamming becomes extended Hamming : single-error correction, double-error detection (SECDED). Let be the original -bit Hamming syndrome over the first seven positions and let be the parity of all eight received bits.
| Hamming syndrome | Overall parity | Meaning within the two-error contract | Action |
|---|---|---|---|
| zero | 0 | no error | deliver data |
| nonzero | 1 | one error at the position named by | flip that position, then verify |
| zero | 1 | error in the added overall-parity bit | flip the overall-parity bit |
| nonzero | 0 | two errors | report uncorrectable; do not alter data |
The last row is the feature the unextended code lacked. Two errors preserve even overall parity but disturb at least one original parity equation, so the decoder detects rather than “corrects” them.
Puncturing the added overall-parity coordinate returns the original Hamming code. This is a useful sanity check: removing one coordinate can reduce distance by at most one, and here it reduces four to three.
Three bounds that answer different questions #
Let
be the number of binary words inside a Hamming ball of radius . Let be the largest possible number of length- binary codewords with minimum distance at least .
| Bound | Statement | Mental model | Engineering use |
|---|---|---|---|
| Hamming / sphere packing | , | guaranteed-correction balls cannot overlap | rejects a request for too many codewords at a claimed correction radius |
| Singleton | for a linear code, | delete any coordinates; distinct codewords must remain distinct | a fast redundancy floor: at least check symbols are necessary |
| Gilbert–Varshamov | greedily add codewords until radius- balls cover the space | proves that a code of at least some size exists; it does not supply a practical decoder |
For Hamming , the sphere-packing calculation is exact:
That equality says the code is perfect, not that it has the largest possible distance for rate . Singleton only gives , so it is looser here. The bounds bracket what parameters are possible; they say nothing by themselves about encoder wiring, decoder complexity, soft-decision performance, burst tolerance, or the probability of patterns beyond the guaranteed radius.
The linear Gilbert–Varshamov form is slightly sharper than the unrestricted greedy count. A sufficient existence condition for a binary linear code is
Treat “exists” literally. A randomly chosen matrix may have the promised distance with high probability at large block length while still being useless to an implementation that cannot search exponentially many codewords.
Shortening, puncturing, and extension #
These operations produce related lengths without redesigning everything, but they change different parts of the contract.
| Operation | Construction | Typical parameter effect | Sharp edge |
|---|---|---|---|
| shorten | restrict one independent information coordinate to zero, then delete its code coordinate | [n−1,k−1,d′] with d′≥d | both ends must agree which logical information coordinate was fixed and omitted |
| puncture | delete one transmitted code coordinate from every codeword | usually [n−1,k,d′] with d′≥d−1 | distance and syndrome mapping change; dimension stays k only when deletion is injective |
| extend by parity | append overall parity to force even weight | odd d becomes d+1; even d stays d | the new parity position and convention are protocol fields, not decoration |
Shortening is not puncturing. A shortened encoder removes one degree of freedom and one transmitted coordinate; a punctured encoder normally removes only a transmitted coordinate. A receiver that inserts the omitted shortened bit in the wrong information position can produce valid syndromes and wrong payloads, making this an interoperability failure rather than an obvious decoder failure.
Implementation and interoperability checklist #
Write down the bit-exact code before optimizing XOR trees:
- exact parameters and whether “Hamming ” includes an overall parity bit;
- the complete and , or normative parity equations from which both are generated;
- information, check, and overall-parity coordinate numbers, including serialization order and bit endianness;
- even- or odd-parity convention and the syndrome-bit significance order;
- every shortened, punctured, or fixed coordinate and the stage at which it is removed or reinserted;
- decoder policy for no error, corrected data, detected-uncorrectable data, and patterns outside the guarantee;
- whether corrected storage words are written back, merely corrected on the read path, or left for a separate scrubber;
- whether downstream logic receives invalid data alongside an error flag or the transaction fails closed.
A good implementation has one machine-readable source for the matrix and derives hardware, software, reference models, and tests from it. OpenTitan's current SECDED tooling is a concrete example: one configuration generates RTL encoders/decoders, C and SystemVerilog reference functions, testbench material, and formal-verification artifacts. Its OTP integration also distinguishes correctable and uncorrectable outcomes instead of reducing both to a single “ECC error” bit. That separation is part of the system contract, not an optional logging detail.
For extended Hamming , exhaustive tests are small enough to be mandatory:
- encode all 16 messages and verify plus even overall parity;
- inject each of the 8 single-bit errors into every codeword and require exact payload recovery plus a corrected status;
- inject all double-bit errors into every codeword and require an uncorrectable status with no attempted correction;
- compare golden wire-order vectors across RTL, firmware, and a simple matrix reference;
- inject representative three- and four-bit patterns to verify the documented failure policy, never an unsupported correction claim;
- test shortened or punctured profiles at every frame boundary and reset condition.
The decoder should re-evaluate parity after a correction in verification builds. If that assertion fails on a nominal single-error test, the likely problem is not coding theory; it is a column order, parity convention, or syndrome-bit significance mismatch.
Scope boundary #
Hamming codes expose the core linear-block-code mechanism in its smallest complete form: a null-space definition, a syndrome, a coset leader, and a distance guarantee. Larger useful families add structure so that the same ideas remain decodable without a table that grows as . Generator polynomials, cyclic shifts, finite-field roots, BCH designed distance, Reed–Solomon symbols, and their algebraic decoders are deliberately reserved for cyclic and BCH codes. Trellis memory and sequence decoding are instead covered by convolutional codes and Viterbi decoding.
Sources
- paperHamming, Error Detecting and Error Correcting Codes (BSTJ 29(2), 1950)
- paperSingleton, Maximum Distance Q-Nary Codes (IEEE Transactions on Information Theory 10(2), 1964)
- paperGilbert, A Comparison of Signalling Alphabets (BSTJ 31(3), 1952)
- paperVarshamov, Estimate of the Number of Signals in Error-Correcting Codes (Doklady 117(5), 1957)
- docsMIT 18.310 Hamming Code Notes
- docsCMU 15-859V Notes 2: Gilbert-Varshamov Bound
- docsOpenTitan Design Tooling: ECC Generator
- docsOpenTitan SRAM Controller: ECC Error Handling and Correction
- codeOpenTitan OTP macro Hamming SECDED integration