Coding Theory
Coding Theory
1 Introduction
Coding theory explores how to transmit data reliably across noisy communication channels.
This theory was inspired by the fundamental paper of Claude Shannon (1948), Marcel Golay
(1949), and Richard Hamming (1950). It uses concepts from algebraic structures, probability,
and combinatorics.
1.1 Message
A message is defined as a finite sequence of characters from a finite alphabet.
1.2 Word
In binary coding theory, the alphabet is usually taken to be the binary set:
B = {0, 1}.
Every character or symbol that we want to transmit is now represented as a sequence of m
elements from B. This binary string is called a word. Thus, our basic unit of information,
called a word, is a sequence of m 0′ s and 1′ s.
1.4 Notation
A element (b1 , b2 , b3 , . . . , bm ) in Zm 5
2 is denoted by b1 b2 b3 . . . bm . For example, (1, 0, 1, 1, 0) ∈ Z2
is simply denoted by 10110.
1
1.5 Properties of Zm
2 :
• Identity element: e = (0, 0, . . . , 0)
• Number of elements: 2m
For instance, inverse of 10110 in Z52 is 10110. Since 10110 ⊕ 10110 = 00000
xt ̸= x.
Hence, we want to develop a technique to help us detect and perhaps even correct transmis-
sion errors. However, we can only improve the chance of correct transmission; there are no
guarantees.
• The channel is symmetric because the error probability is the same for 0 and 1.
2
2.1 Probability of Errors
Consider the following example:
Let
c = 10110 ∈ Z52
be the transmitted code, and
r = 00110 ∈ Z52
• The positions of 1’s in e indicate the locations of errors in the received code.
• The number of 1’s in e corresponds to the number of bit errors during transmission.
General Case
Let
c ∈ Zn2
be a transmitted codeword, and
r =c⊕e
be the received codeword, where:
3
Theorem 2.1. Let c ∈ Zn2 be the transmitted codeword through BSC with probability p of
incorrect transmission.
P (r = c ⊕ e) = pk (1 − p)n−k
Example 2.3. Let C be a set of codewords, where C ⊆ Zn2 . In each of the following cases,
two of the three values are given: the codeword c, the received word r, and the error pattern
e. Using the relation
r = c ⊕ e,
determine the missing term.
Answer (a):
e = r ⊕ c = 1011111 ⊕ 1010110 = 0001001
Answer (b):
r = c ⊕ e = 1010110 ⊕ 0101101 = 1111011
Answer (c):
c = r ⊕ e = 0000111 ⊕ 0101111 = 0101000
Example 2.4. A binary symmetric channel has probability p = 0.05 of incorrect transmis-
sion per bit. Let the codeword
c = 011011101
be transmitted. What is the probability of the following events?
4
(b) We receive r = 111011100.
(d):
9
P = · (0.05)2 · (0.95)7 = 36 · 0.0025 · 0.698 ≈ 0.0629
2
(e):
9
P = · (0.05)3 · (0.95)6 = 84 · 0.000125 · 0.735 ≈ 0.0077
3
D : Zn2 →, W
5
• Ideally, D ◦ T ◦ E should be the identity on W , but this is not always possible due to
errors. The goal is to design E and D so that the probability of correctly decoding is
high.
• Block code The ordered pair (n, m) is called block code of E and D.
• Parity-Check code
• Observe that wm+1 is zero if and only if the number of 1’s in w is even. Hence, every
code word E(w) has even weight.
• A single-bit error in the transmission of a code word will change its weight to odd,
which can be detected.
6
• Similarly, any odd number of bit errors can be detected by this code.
• The scheme does not detect errors if an even number of bits are altered
• If an error is detected, the codeword is retransmitted and continue the process until
the correct code is received or received code has an even number of 1′ s.
c = E(w) = 110101101.
If the transmission channel sends this as T (c) = 100101101, we compute the weight wt(T (c)) =
5, which is odd. Therefore, we conclude that an error (or an odd number of errors) has oc-
curred.
Example 3.2. For the given codes, apply (i) the even parity check, (ii) the odd parity check
codes.
(a) w = 1011
(b) w = 1010
(c) w = 101101
(d) w = 110111
Answer:
Label Original Code (w) Even Parity Odd Parity
(a) 1011 10111 10110
(b) 1010 10100 10101
(c) 101101 1011010 1011011
(d) 110111 1101111 1101110
Example 3.3. Apply the odd parity check to the code 101101. Determine if the received
code is correct and whether the error is detected.
(a) r = 1011011
(b) r = 1111011
(c) r = 1001111
7
Answer:
Trade-off:
Adding extra bits reduces the chance of undetected error but also reduces the rate of the
code
Example 3.5. Suppose 160 bits are sent in successive strings of length 8. Find the proba-
bility of receiving the correct message:
8
(i) without any coding scheme,
(ii) with the parity-check method.
Assume p = 0.001.
Solution:
(i) Since there are 160 bits and each string is of length 8, there are 160/8 = 20 strings.
Therefore, Probability of receiving the correct message without any coding scheme
(ii) Probability with the parity-check method: In parity-check, each 8-bit string becomes
a 9-bit code word with error detection.
A 9-bit word is correct if either:
• all 9 bits are received correctly: (0.999)9 ,
• exactly 1 bit is incorrect and it is detected and corrected: 9 · (0.001) · (0.999)8 .
So the total probability per word:
Example 3.6. Suppose the message w = 110101 is sent. Find the probability of receiving
the correct message:
(i) without any coding scheme,
(ii) with the parity-check method.
Assume p = 0.05.
Solution:
(i) Probability of receiving the correct code without any coding method:
(ii) In the parity-check method, we append a bit to make the number of 1’s even. For
w = 110101, number of 1’s is 4 (even), so we append a 0:
9
3.2 Triple repetition code
The (3m, m) block code is called triple repetition code if for any w = w1 w2 w3 . . . wm ∈ Zm
2 ,
• Define
E : Zm 3m
2 → Z2 by E(w) = w1 w2 . . . wm w1 w2 . . . wm w1 w2 . . . wm
c = E(w) = 101101111011101110111011.
If we receive:
T (c) = 101001110011011110110110,
we decode each group of three bits by majority:
• Fourth triplet: 0, 0, 0 ⇒ 0
• Fifth triplet: 0, 1, 1 ⇒ 1
• Sixth triplet: 1, 1, 1 ⇒ 1
• Seventh triplet: 1, 1, 1 ⇒ 1
• Eighth triplet: 0, 1, 0 ⇒ 0
10110111
Error Analysis
Even though multiple bit errors occurred, as long as at most one error happens in each group
of three, the original bit can be correctly recovered. However, if two or more errors occur in
the same group (e.g., at positions 2, 10, and 18), decoding may fail.
10
Probability of Correct Decoding
Let p = 0.001 be the probability of a bit error. Then, the probability of correctly decoding
a single bit using triple repetition is:
3 3
Pcorrect = (1 − p) + (p)(1 − p)2
1
3 3
= (0.999) + (0.001)(0.999)2 ≈ 0.99999700.
1
(0.99999700)8 ≈ 0.99997600.
This is slightly better than the parity-check code, but note that it requires transmitting
24 bits instead of 8, so the transmission rate is 13 . While transmission time increases, we
avoid retransmissions and gain the ability to correct errors—not just detect them.
Conclusion
• The triple repetition code is effective at correcting single-bit errors with high proba-
bility.
• Retransmission is avoided.
Example 3.7. Let E : Z32 → Z92 be the encoding function for the (9, 3) triple repetition
code.
(a) Let D : Z92 → Z32 be the corresponding decoding function. Apply D to decode the
received words:
(i) r = 111101100
(ii) r = 010011111
(b) Find three different received words r for which D(r) = 000.
Solution (a):
(i)
11
(ii)
Solution (b):
For D(r) = 000, in each of the positions:
Solution (c):
D : Z92 → Z32
|Z92 | = 512, |Z32 | = 8
|Z9 | 512
⇒ D−1 (w) = 32 = = 64
|Z2 | 8
Example 3.8. The (5m, m) five-times repetition code has encoding function E : Zm2 → Z2 ,
5m
(a) With p = 0.05, what is the probability for the transmission and correct decoding of
the signal 0?
(b) Answer part (a) for the message 110 in place of the signal 0.
Solution:
12
(a) Here w = 0, ⇒ E : Z12 → Z52 , ⇒ E(w) = 00000
then D : Z52 → Z12 .
With p = 0.05, the probability of correctly decoding a single bit is (by majority rule
we can make at most 2 errors out of 5 positions):
5 5 4 5
(0.95) + (0.05)(0.95) + (0.05)2 (0.95)3 = 0.998842
1 2
So the probability of receiving and correctly decoding the one-bit message is:
(0.998842)1 = 0.998842
So the probability of receiving and correctly decoding the three-bit message is:
(0.998842)3 = 0.996530
(e) m = 2, D : Z10 2
2 → Z2
13
4 Hamming metric
Definition 4.1 (Weight). For any x = x1 x2 . . . xn ∈ Zn2 , the weight of x, denoted by wt(x),
is the number of 1’s in x: n
X
wt(x) = xi .
i=1
Proof. This follows because bitwise addition modulo 2 does not introduce any new 1’s. The
only case where xi ⊕2 yi = 1 is when exactly one of xi or yi is 1.
• wt(01001) = 2
• wt(11101) = 4
Definition 4.4 (Distance). The Hamming distance between x, y ∈ Zn2 is defined as:
n
(
X 0 if xi = yi ,
d(x, y) = d(xi , yi ), where d(xi , yi ) =
i=1
1 if xi ̸= yi .
Note:
d(x, y) = wt(x ⊕ y).
This is because in Z2 , xi ⊕2 yi = 1 if and only if xi ̸= yi .
1. d(x, y) ≥ 0
2. d(x, y) = 0 ⇔ x = y
3. d(x, y) = d(y, x)
14
4.1 Hamming Metric
The function d(x, y) defined on Zn2 is a distance function or metric, and the space (Zn2 , d)
is called a metric space. It captures the idea of closeness between binary vectors.
Definition 4.6 (Sphere of Radius k). For a vector x ∈ Zn2 , the sphere of radius k
centered at x is:
S(x, k) = {y ∈ Zn2 : d(x, y) ≤ k}
1. We can detect all transmission errors of weight ≤ k iff the minimum distance between
code words is at least k + 1.
2. We can correct all transmission errors of weight ≤ k iff the minimum distance between
code words is at least 2k + 1.
E(00) = 000000,
E(10) = 101010,
E(01) = 010101,
E(11) = 111111.
1. Compute Hamming distances between code words and hence find minimum distance
between code words.
d(c1 , c2 ) = 3,
d(c1 , c3 ) = 3,
d(c1 , c4 ) = 6,
d(c2 , c3 ) = 6,
d(c2 , c4 ) = 3,
d(c3 , c4 ) = 3.
15
The minimum distance between the codewords is 3.
Solution 2: By Theorem 4.8 (1), k + 1 = 3 ⇒ k = 2; and by Theorem 4.8 (2), 2k + 1 =
3 ⇒ k = 1.
Hence, we can:
• Detect up to 2 errors,
• Correct up to 1 error.
Example 4.10. Let W = Z22 and E : W → Z62 be defined by:
E(00) = 000000,
E(10) = 101010,
E(01) = 010101,
E(11) = 111111.
(i) 110101
(ii) 101011
(iii) 001111
(iv) 110000
Solution 1:
S(101010, 1) = {x ∈ Z62 | d(x, 101010) ≤ 1}:
Solution 2:
(i) 110101
Majority at 1st, 3rd, 5th positions: 0
Majority at 2nd, 4th, 6th positions: 1
⇒ Decoded word is: 01
(ii) 101011
Majority at 1st, 3rd, 5th positions: 1
Majority at 2nd, 4th, 6th positions: 0
⇒ Decoded word is: 10
16
(iii) 001111
Majority at 1st, 3rd, 5th positions: 1
Majority at 2nd, 4th, 6th positions: 1
⇒ Decoded word is: 11
(iv) 110000
Majority at 1st, 3rd, 5th positions: 0
Majority at 2nd, 4th, 6th positions: 0
⇒ Decoded word is: 00
Note:
Suppose c = 010101 and T (c) = r = 111101. Then:
• r is not a codeword, so a double error can be detected.
However, if T (c) = r1 = 111111 (a triple error), then:
• We incorrectly decode r1 as the codeword 111111, assuming c = 11 instead of the
correct message 01.
Example 4.11. Answer the following:
1. If x ∈ Z10
2 , determine |S(x, 1)|, |S(x, 2)|, |S(x, 3)|.
S(x, 1) = y ∈ Z10
2 d(x, y) ≤ 1
= y ∈ Z10
2 d(x, y) = 0 or d(x, y) = 1
S(x, 2) = y ∈ Z10
2 d(x, y) ≤ 2
= y ∈ Z10
2 d(x, y) = 0 or d(x, y) = 1 or d(x, y) = 2
For d(x, y) = 2, y should differ from x at only two positions, and there are C(10, 2) = 45
possibilities.
|S(x, 2)| = 1 + 10 + 45 = 56
Similarly,
|S(x, 3)| = 1 + C(10, 1) + C(10, 2) + C(10, 3) = 176
17
Solution 2:
With 1 ≤ k ≤ n, for x ∈ Zn2 ,
k
X
|S(x, k)| = 1 + C(n, 1) + C(n, 2) + · · · + C(n, k) = C(n, r)
r=0
Example 4.12. Let E : Z52 → Z25 2 be an encoding function where the minimum distance
between code words is 9. What is the largest value of k such that we can detect errors of
weight ≤ k? If we wish to correct errors of weight ≤ t, what is the maximum value of t?
Soln:
We can use Theorem 4.8 (1) to find k: k+1=9⇒k =8
We can use Theorem 4.8 (2) to find t: 2t + 1 = 9 ⇒ t = 4
Example 4.13. For each of the following encoding functions, find the minimum distance
between the code words. Discuss the error-detecting and error-correcting capabilities of each
code.
1. E : Z22 → Z52
00 → 00001
01 → 01010
10 → 10100
11 → 11111
2. E : Z22 → Z10
2
00 → 0000000000
01 → 0000011111
10 → 1111000000
11 → 1111111111
3. E : Z32 → Z62
000 → 000011
001 → 001001
010 → 010010
011 → 011100
100 → 100100
101 → 101010
110 → 110001
111 → 111000
18
4. E : Z32 → Z82
000 → 00011111
001 → 00111010
010 → 01010101
011 → 01110000
100 → 10001101
101 → 10101000
110 → 11000100
111 → 11100011
Solution 1:
r1 = 0000000000,
r2 = 0000011111,
r3 = 1111000000,
r4 = 1111111111
d(r1 , r2 ) = 5,
d(r1 , r3 ) = 5,
d(r1 , r4 ) = 10,
d(r2 , r3 ) = 10,
d(r2 , r4 ) = 5,
d(r3 , r4 ) = 5
19
The code can detect all errors of weight ≤ 4.
Applying Theorem 2: 2k + 1 = 5 ⇒ k = 2
1
The code can correct all errors of weight ≤ ⇒ the code cannot correct any error.
2
20
Introduction
In coding theory, generator matrices and parity-check matrices help encode and decode
messages using binary linear codes over Z2 . These matrices are useful in locating the nearest
codeword to a received word, and they play a vital role in error detection and correction.
Generator Matrix G
A generator matrix G is used to encode a message vector into a codeword.
Let G be a k × n matrix over Z2 , and let w ∈ Zk2 be a message (row vector). The encoded
codeword is:
E(w) = wG ∈ Zn2
Example
Let
1 0 0 1 1 0
G = 0 1 0 0 1 1
0 0 1 1 0 1
This is a 3 × 6 matrix. The first 3 columns form the identity matrix I3 , and the last 3
columns form matrix A. Thus,
G = [I3 | A]
This structure ensures systematic encoding, meaning the original message appears
directly in the codeword.
Codewords
Each message w ∈ Z32 produces a codeword in Z62 .
For
W = {000, 100, 010, 001, 110, 101, 011, 111}
The set of codewords is:
21
Parity-Check Matrix H
The parity-check matrix H is used to verify if a received word is a valid codeword.
Given A as the right part of G, define:
H = [AT | In−k ]
In our example:
1 1 0 1 0 1 1 0 0
A = 0 1 1 ⇒ H = 1 1 0 0 1 0
1 0 1 0 1 1 0 0 1
Syndrome Decoding
Given a received word r ∈ Z62 , compute the syndrome:
syndrome(r) = H · rT
Example (Continued...)
Suppose r = 110110. Then,
1
1
1 0 1 1 0 0
0 0
T
H · r = 1 1 0 0 1 0 · = 1
1
0 1 1 0 0 1 1 1
0
This nonzero result indicates an error. Based on the syndrome, we identify and correct
the bit in error.
d>k
d ≥ 2t + 1
22
In Our Example
• Minimum distance, d = 3
Multiple Errors
Suppose we receive r = 000111. Then,
0
0
1 0 1 0 0 0
0 1
T
H · r = 1 1 0 1 0 0 = 1 ,
1 which is not a column of H.
0 1 1 0 0 1
1 1
1
Yet
• If H · rT came from the first and sixth columns of H, correcting these components in
r results in the code word 100110.
• If we sum the third and fifth columns of H to get this syndrome, upon changing the
third and fifth component of r we get a second code word, 001101.
2k + 1 = 3 ⇒ k = 1,
23
Example 4.14. Let E : Z32 → Z62 .
Solution a):
Let r1 = 111101 Consider H · r1T :
1
1
1 0 1 1 0 0 1 1
1 1 0 0 1 0 1 = 0 ⇒ H · r1T = 0
1
0 1 1 0 0 1
0 1 1
1
Similarly, we can obtain all other decoded words.
The decoded words are:
Considering
Received Correction
Decoded Word First 3 Bits (De-
Word at Position
coded Word)
(i) 111101 3rd 110101 110
(ii) 110101 nil 110101 110
(iii) 001111 5th 001101 001
(iv) 100100 5th 100110 100
(v) 110001 4th 110101 110
(vi) 111111 1st & 6th 011110 (other possibilities) 011
(vii) 111100 4th 111000 (other possibilities) 111
(viii) 010100 1st & 6th 110101 (other possibilities) 110
Solution b): No, not all results are uniquely determined, (vi) and (viii) received words
have other possibilities.
Example 4.15. The encoding function E : Z22 → Z52 is given by the generator matrix
1 0 1 1 0
G=
0 1 0 1 1
24
(a) Determine all code words. What can we say about the error-detection capability of
this code? What about its error-correction capability?
i) 11011
ii) 10101
iii) 11010
iv) 00111
v) 11101
vi) 00110
Solution a):
Let W = {00, 10, 01, 11}. Then
00 00000
10 1 0 1 1 0 10110
c = wG =
01 0 1 0 1 1 =
01011
11 11101
So the code is: C = {00000, 10110, 01001, 11101}.
Let c1 = 00000, c2 = 10110, c3 = 01001, c4 = 11101
The Hamming distance between code words are:
25
ii) : r = 10101, s = 011 ⇒ error at position 2 ⇒ decode to 11101
iii) : r = 11010, s = 111 ⇒ error at position 1 and 5 ⇒ decode to 01011
iv) : r = 00111, s = 111 ⇒ error at position 1 and 5 ⇒ decode to 10110
v) : r = 11101, s = 000 ⇒ valid codeword
vi : r = 00110, s = 110 ⇒ error at position 1 ⇒ decode to 10110
Considering the first 2 bits (message), the decoded words are:
01, 11, 01, 10, 11, 10
Example 4.16. Define the encoding function E : Z32 → Z62 by means of the parity-check
matrix
1 0 1 1 0 0
H = 1 1 0 0 1 0
1 0 1 0 0 1
(a) Determine all code words.
(b) Does this code correct all single errors in transmission?
Solution a):
Any x = x1 x2 x3 x4 x5 x6 ∈ Z62 is a codeword if and only if HxT = 0.
Consider
x1
x2
1 0 1 1 0 0
x3 0
T
Hx = H = 1 1 0 0 1 0 = 0
x4
1 0 1 0 0 1
x5 0
x6
This implies the equations:
x1 + x3 = x4
x1 + x2 = x5
x1 + x 3 = x6
Possible combinations:
x1 x2 x3 x4 x5 x6 Codeword Label
0 0 0 0 0 0 000000 c1
1 0 0 1 1 1 100111 c2
0 1 0 0 1 0 010010 c3
0 0 1 1 0 1 001101 c4
1 1 0 1 0 1 110101 c5
1 0 1 0 1 0 101010 c6
0 1 1 1 1 1 011111 c7
1 1 1 0 0 0 111000 c8
26
Solution b):
Hamming distance table d(ci , cj ):
c1 c2 c3 c4 c5 c6 c7 c8
c1 − 4 2 3 4 3 5 3
c2 4 − 4 3 2 3 3 5
c3 2 4 − 5 4 3 3 3
c4 3 3 5 − 3 4 2 4
c5 4 2 4 3 − 5 3 3
c6 3 3 3 4 5 − 4 2
c7 5 3 3 2 3 4 − 4
c8 3 5 3 4 3 2 4 −
The minimum distance = 2. Therefore:
27