Mathwords logoMathwords

Linear Code — Definition, Formula & Example

A linear code is an error-correcting code in which adding any two codewords, digit by digit, always produces another codeword. Because of this structure, you can describe the whole code with one small matrix instead of listing every codeword.

A linear [n,k][n, k] code over the finite field Fq\mathbb{F}_q is a kk-dimensional subspace CC of the vector space Fqn\mathbb{F}_q^n. Equivalently, CC is the row space of a k×nk \times n generator matrix GG of rank kk, or the null space of an (n−k)×n(n-k) \times n parity-check matrix HH. The minimum distance dd of CC equals the smallest Hamming weight of any nonzero codeword. Such a code is written as an [n,k,d][n, k, d] code.

Key Formula

∣C∣=qk,d=min⁡c∈C, c≠0wt⁡(c)|C| = q^{k}, \qquad d = \min_{\mathbf{c} \in C,\ \mathbf{c} \neq \mathbf{0}} \operatorname{wt}(\mathbf{c})
Where:
  • ∣C∣|C| = number of codewords in the code
  • qq = size of the alphabet (the field), so q = 2 for binary codes
  • kk = dimension of the code, which is the number of message symbols
  • dd = minimum distance of the code
  • wt⁡(c)\operatorname{wt}(\mathbf{c}) = Hamming weight of c, the number of nonzero entries in c

Worked Example

Problem: Is the binary code C={000, 110, 101, 011}C = \{000,\ 110,\ 101,\ 011\} linear? If it is, give a generator matrix, its dimension kk, its minimum distance dd, and how many bit errors it can detect and correct.
Step 1: A binary code is linear when it contains the zero word and the sum (mod 2) of any two codewords is again a codeword. Check every sum of two different nonzero codewords.
110+101=011,110+011=101,101+011=110110 + 101 = 011, \qquad 110 + 011 = 101, \qquad 101 + 011 = 110
Step 2: Every sum lands back in CC, and 000∈C000 \in C, so CC is a subspace of F23\mathbb{F}_2^3. The two words 110110 and 101101 are independent and generate the other two, so they form the rows of a generator matrix. The dimension is k=2k = 2, which matches ∣C∣=22=4|C| = 2^2 = 4.
G=[110101],k=2G = \begin{bmatrix} 1 & 1 & 0 \\ 1 & 0 & 1 \end{bmatrix}, \qquad k = 2
Step 3: Because the code is linear, dd is the smallest weight of a nonzero codeword. Every nonzero codeword has exactly two 1s. A code with minimum distance dd detects d−1d - 1 errors and corrects ⌊(d−1)/2⌋\lfloor (d-1)/2 \rfloor errors.
d=2,detects 2−1=1,corrects ⌊2−12⌋=0d = 2, \qquad \text{detects } 2 - 1 = 1, \qquad \text{corrects } \left\lfloor \tfrac{2-1}{2} \right\rfloor = 0
Answer: CC is a binary [3,2,2][3, 2, 2] linear code, the even-weight (single parity-check) code. It detects any single bit error but cannot correct it.

Why It Matters

Encoding with a linear code takes one matrix multiplication, and decoding can use a short syndrome computed with the parity-check matrix. You don't need a lookup table of every codeword. This efficiency is why Reed–Solomon codes protect data on QR codes and CDs, and why linear codes are a standard topic in abstract algebra and information theory courses.

Common Mistakes

Mistake: Adding codewords with ordinary arithmetic, which produces digits like 2 that cannot appear in a binary code.
Correction: In a binary linear code, addition happens in the field with two elements. Add each position separately and reduce mod 2, so 1 + 1 gives 0 and nothing carries into the next position.

Related Terms