Coding Techniques and Error Detection
Coding Techniques and Error Detection
Chapter 4 – Coding 1 / 85
Overview
1 Coding information
Binary Representation
Octal Representation
Hexadecimal Representation
Error-detecting Codes
ISBN
Chapter 4 – Coding 2 / 85
Coding Information
Chapter 4 – Coding 3 / 85
Coding Information
Chapter 4 – Coding 4 / 85
Coding Information
Chapter 4 – Coding 5 / 85
Coding Information
Chapter 4 – Coding 6 / 85
Binary Representation
Chapter 4 – Coding 7 / 85
Binary Representation
“0” or “1”
Chapter 4 – Coding 8 / 85
Binary Representation
Decimal system:
Historically (and physically), counting is usually done in groups of tens.
Hence positive numbers are encoded by ten basic symbols (digits):
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Example 4.1
2014 = 2 × 103 + 0 × 102 + 1 × 101 + 4 × 100 .
Chapter 4 – Coding 9 / 85
Binary Representation
Binary system:
Counting can also be done in groups of twos. Positive numbers may be
coded by using only two basic symbols: 0, 1.
Example 4.2
0 = 000 4 = 100
1 = 001 5 = 101
2 = 010 6 = 110
3 = 011 7 = 111
Chapter 4 – Coding 10 / 85
Binary Representation
N = rk × 2k−1 + · · · + r2 × 21 + r1 × 20
Chapter 4 – Coding 11 / 85
Binary Representation
(rk rk−1 . . . r2 r1 )2
Chapter 4 – Coding 12 / 85
Binary Representation
Example 4.3
Covert 235 into binary representation.
Solution:
235 ÷ 2 = 117 . . . 1
117 ÷ 2 = 58 . . . 1
58 ÷ 2 = 29 . . . 0
29 ÷ 2 = 14 . . . 1
14 ÷ 2 = 7 . . . 0
7 ÷ 2 = 3...1
3 ÷ 2 = 1...1
1 ÷ 2 = 0...1
Therefore 235 = (11101011)2 .
Chapter 4 – Coding 13 / 85
Binary Representation
Example 4.4
43 = 1 × 25 + 0 × 24 + 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20
= (101011)2
43 ÷ 2 = 21 . . . 1
21 ÷ 2 = 10 . . . 1
10 ÷ 2 = 5 . . . 0
5 ÷ 2 = 2...1
2 ÷ 2 = 1...0
1 ÷ 2 = 0...1
Chapter 4 – Coding 14 / 85
Binary Representation
Chapter 4 – Coding 15 / 85
Binary Representation
Chapter 4 – Coding 16 / 85
Octal Representation
Octal representation:
A representation of positive integers using the following 8 digits:
0, 1, 2, 3, 4, 5, 6, 7
Chapter 4 – Coding 17 / 85
Octal Representation
N = rk × 8k−1 + · · · + r2 × 81 + r1 × 80
(rk rk−1 . . . r2 r1 )8
Chapter 4 – Coding 18 / 85
Octal Representation
Example 4.5
Find the octal representation of 106.
Solution.
106 ÷ 8 = 13 . . . 2
13 ÷ 8 = 1 . . . 5
1 ÷ 8 = 0...1
So 106 = (152)8 .
Chapter 4 – Coding 19 / 85
Hexadecimal Representation
Hexadecimal representation:
A representation of positive integers using the following 16 symbols:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C , D, E , F
Chapter 4 – Coding 20 / 85
Hexadecimal Representation
Chapter 4 – Coding 21 / 85
Hexadecimal Representation
Example 4.6
Find the hexadecimal form of 3436.
Solution.
3436 ÷ 16 = 214 . . . 12
214 ÷ 16 = 13 . . . 6
13 ÷ 16 = 0 . . . 13
Thus,
3436 = (D6C )16 .
Chapter 4 – Coding 22 / 85
Error-detecting Codes
Chapter 4 – Coding 23 / 85
Error-detecting Codes
Chapter 4 – Coding 24 / 85
Error-detecting Codes
These letters can detect simple errors in writing the numbers and can
prevent forgery.
In “Monday, 01-10-2001”, the day of the week (in this case, Monday)
can also serve as a “check symbol”. For instance, it cannot be 10 Jan
2001, which is a Wednesday. Inconsistency will also show up for certain
errors in the day, month or year.
Chapter 4 – Coding 25 / 85
Encoding
Information contains letters, blank space, and digits
Each symbol is given the corresponding numerical value:
Symbol 0 1 2 3 4 5 6 7 8 9
Numerical 0 1 2 3 4 5 6 7 8 9
Symbol A B C D E F G H I J
Numerical 10 11 12 13 14 15 16 17 18 19
Symbol K L M N O P Q R S T
Numerical 20 21 22 23 24 25 26 27 28 29
Symbol U V W X Y Z blank
Numerical 30 31 32 33 34 35 36
Chapter 4 – Coding 27 / 85
Weighted sum
Example 4.8
Find the weighted sum of the sequence NO FOOD.
Chapter 4 – Coding 28 / 85
Encoding
Encoding procedure modulo 37:
Sn Sn−1 · · · S2 S1 .
Chapter 4 – Coding 29 / 85
Encoding
Remark
More generally, the number 37 can be replaced by any prime number p.
Then we’ll need to have n ≤ p − 1 so that after the check digit has been
appended, the length is ≤ p − 1. For the time being, we shall keep to 37.
Chapter 4 – Coding 30 / 85
Encoding
Example 4.9
Encode A6 7 modulo 37.
Chapter 4 – Coding 31 / 85
Error detection
Definition 4.10
A word is said to contain k errors if k of its letters are erroneous.
Chapter 4 – Coding 32 / 85
Error detection
Remark
If A is a correctly encoded word, then its weighted sum W (A) ≡ 0
(mod 37).
Chapter 4 – Coding 33 / 85
Error detection
Error detection
If A is a correctly encoded word, and during transmission, some errors
occur and the word A0 is received. The errors in A0 is said to be detected if
its weighted sum
W (A0 ) 6≡ 0 (mod 37).
Chapter 4 – Coding 34 / 85
Error detection
Example 4.11
Suppose the encded word A6 7Q is received as C 6 7Q, then the error is
detected as the weighted sum
Chapter 4 – Coding 35 / 85
Error detection
Theorem 4.12
In weighted sum encoding modulo 37, if a single error occurs, then it can
be detected.
Proof:
Suppose A = Sn . . . S1 is the correct word and a single error error occurs at
position i, changing Si to x and the word A to A0 . Then
Since 0 < i < 37 and 0 < |Si − x| < 37, the product i(Si − x) cannot be a
multiple of 37. Thus W (A) − W (A0 ) 6≡ 0 (mod 37). But W (A) ≡ 0
(mod 37). Therefore W (A0 ) 6≡ 0 (mod 37) and the error is detected.
Chapter 4 – Coding 36 / 85
Transposition error
Example 4.13
Suppose an encoded word is wxyz, where x and y are distinct symbols and
z is the check symbol.
Chapter 4 – Coding 37 / 85
Transposition error
Solution. Let A = wxyz, B = wyxz and let W (A) and W (B) denote
their respective weighted sum.
The error can be detected if we can show that W (B) 6≡ 0 (mod 37).
Chapter 4 – Coding 38 / 85
Transposition error
By definition,
W (A) = 4w + 3x + 2y + z,
W (B) = 4w + 3y + 2x + z.
Chapter 4 – Coding 39 / 85
Transposition error
Theorem 4.14
Weighted sum modulo 37 can detect a transposition error.
Proof: Suppose in the word A, the number at position i and j are a and b
respectively, and they have been interchanged to yield an incorrect word B.
Chapter 4 – Coding 40 / 85
Transposition error
Then
Chapter 4 – Coding 41 / 85
ISBN
The ISBN is
13 digits long if assigned on or after 1 January 2007
10 digits long if assigned before 2007
Chapter 4 – Coding 42 / 85
ISBN
The digits are arranged in 4 groups, which are sometimes (but not always)
separated by hyphens.
Geographical grouping (country)
Publisher
Identification of the book (title)
Check digit
Chapter 4 – Coding 43 / 85
Chapter 4 – Coding 44 / 85
ISBN
The check digit is chosen so that the weighted sum of all the 10
digits is a multiple of 11.
The check digit could be
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, X
Chapter 4 – Coding 45 / 85
ISBN
Example:
0 − 1321 − 2571 − 4
Chapter 4 – Coding 46 / 85
ISBN
Example 4.15
Check that the number 0 − 1315 − 2447 − X is a proper ISBN.
Chapter 4 – Coding 47 / 85
ISBN
Example 4.16
Suppose the ISBN 1 − 886544 − 45 − X is copied incorrectly with the digit
‘6’ replaced by ‘9’. How does this affect the weighted sum?
Chapter 4 – Coding 48 / 85
ISBN
Chapter 4 – Coding 49 / 85
ISBN
Example 4.17
Suppose we encode modulo 10 instead of 11.
If the original ISBN is 1 − 886544 − 45 − c. What is c?
Suppose the first digit of the ISBN above is copied wrongly as 5, and
the rest are copied correctly.
Show that this error cannot be detected.
Chapter 4 – Coding 50 / 85
ISBN
Chapter 4 – Coding 51 / 85
ISBN
Since the weighted sum does not violate the congruence condition, the
error cannot be detected.
Chapter 4 – Coding 52 / 85
ISBN
Theorem 4.18
ISBN can detect a single error and a transposition eror.
Proof: Follows from previous theorems with the prime number 11.
Chapter 4 – Coding 53 / 85
ISBN
What it can do
The two most common errors in handling an ISBN (e.g., typing or writing
it) are a single altered digit or the transposition of adjacent digits. ISBN
can detect these errors.
In other words, if such an error is committed, the result will be an invalid
ISBN.
Chapter 4 – Coding 54 / 85
ISBN
Example 4.19
The ISBN 0 − 1321 − 2571 − 4 is wrongly recorded as 0 − 1321 − 1572 − 4.
The error will be detected as the latter is not a valid ISBN as the weighted
sum is NOT 0 mod 11.
10 · 0 + 9 · 1 + 8 · 3 + 7 · 2 + 6 · 1 + 5 · 1 + 4 · 5 + 3 · 7 + 2 · 2 + 1 · 4 ≡ 4 (mod 11)
Chapter 4 – Coding 55 / 85
ISBN
What it can’t do
It is possible for other types of error, such as two altered non-transposed
digits, or three altered digits, to result in a valid ISBN number (although it
is still unlikely).
Chapter 4 – Coding 56 / 85
Error-correcting codes
Very often, it is desirable to not only detect errors, but also to pinpoint
the errors and correct them.
Assuming not too many errors occur, it is possible to recover the original
message. Here, we see how this can be done using
Hamming (7, 4) Codeword
Hamming (8, 4) Codeword
Chapter 4 – Coding 57 / 85
Richard Hamming
Richard Hamming
Born: February 11, 1915, Chicago, Illinois
Died: January 7, 1998 (aged 82) Monterey, California
Hamming code was discovered in 1948 while he was at Bell Labs.
Chapter 4 – Coding 58 / 85
Hamming (7, 4) Codes
Chapter 4 – Coding 59 / 85
Hamming (7, 4) Codes
Chapter 4 – Coding 60 / 85
Hamming (7, 4) Codes
The parity bits are defined in such a way that the total number of 1’s in
each of the circles A, B and C must be even.
Chapter 4 – Coding 61 / 85
Hamming (7, 4) Codes
Example 4.20
Find the Hamming (7, 4) codeword for the given word w = 0100.
Solution:
Chapter 4 – Coding 62 / 85
Hamming (7, 4) Codes: 1-bit error
Theorem 4.21
Hamming (7, 4) Codes can correct a 1-bit error.
Chapter 4 – Coding 63 / 85
Hamming (7, 4) Codes: 1-bit error
This single-bit error can be detected and corrected based on the parity
check on the circles A, B, C : Recall
A passes the check: s1 + s3 + s4 + s5 ≡ 0 (mod 2)
B passes the check: s1 + s2 + s4 + s6 ≡ 0 (mod 2)
C passes the check: s1 + s2 + s3 + s7 ≡ 0 (mod 2)
Chapter 4 – Coding 64 / 85
Hamming (7, 4) Codes: 1-bit error
The indicated bit is the bit that is not correct.
Chapter 4 – Coding 65 / 85
Hamming (7, 4) Codes: 1-bit error
Chapter 4 – Coding 66 / 85
Hamming (7, 4) Code: 1-bit error
Example 4.22
Assuming at most one error, find the original codeword of the following
Hamming (7, 4) codewords received.
(a) 1010101
(b) 0101000
Solution.
(a) 0010101 (All A, B, C fail the parity check – error in bit 1)
(b) 0111000 (Only A and C fail the parity check) – error in bit 3)
Chapter 4 – Coding 67 / 85
Hamming (7, 4) Codes: 2-bit error
Theorem 4.23
Hamming (7, 4) Codes can detect a 2 bit-error.
Chapter 4 – Coding 68 / 85
Hamming (7, 4) Codes: 2-bit error
Chapter 4 – Coding 69 / 85
Hamming (7, 4) Codes: 2-bit error
Chapter 4 – Coding 70 / 85
Hamming (7, 4) Codes: 2-bit error
Example 4.24
Suppose there are exactly two error bits in the word t1 t2 t3 t4 t5 t6 t7
received using the Hamming (7, 4) code.
Solution. NO. Here is one example: The diagram shows three instances
in which a 2-bit error occurs, with the “crosses” representing the positions
of the error bits in the received word.
Chapter 4 – Coding 71 / 85
Hamming (7, 4) Codes: 2-bit error
In all cases, only A fails the check. At the receiving end, we will not be
able to tell which pairs of errors actually occurred and thus will be unable
to correct the error..
Chapter 4 – Coding 72 / 85
Hamming (7, 4) Codes: erasure error
Example 4.25
Suppose a Hamming (7, 4) codeword was sent and the word received is
1?1?001. Assuming there is no error in the transmitted word, except that
the second and the forth bit were unrecognizable. Such a fault is called an
erasure.
What was the transmitted word?
Chapter 4 – Coding 73 / 85
Hamming (7, 4) Codes: erasure error
Theorem 4.26
Erasure error involving at most 2 bits can always be corrected.
Proof: The given figure shows the 6 typical cases where a • indicates an
erased bit. In all cases, the erased bits can be recovered.
Chapter 4 – Coding 74 / 85
Hamming (7, 4) Codes: erasure error
Chapter 4 – Coding 75 / 85
Hamming (8, 4) Codes
s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 ≡ 0 (mod 2).
The above requirement is known as the overall parity check. It means that
the number of ‘1’ bits is even.
Chapter 4 – Coding 76 / 85
Hamming (8, 4) Codes
Example:
Hamming (8, 4) code for the message 0111 is 01110001. We put the
eighth digit outside of A ∪ B ∪ C .
Chapter 4 – Coding 77 / 85
Hamming (8, 4) Codes
Theorem 4.27
If the overall parity check fails, then there is an odd number of errors.
If the overall parity check passes, then there is an even number (including
0) of errors.
Chapter 4 – Coding 78 / 85
Hamming (8, 4) Codes
Proof. We only need to note that every error causes an odd number of
changes in the number of ‘1’ bits. Thus if the overall parity fails, there
must be an odd number of errors and if it passes, then there must an even
number of errors.
Chapter 4 – Coding 79 / 85
Hamming Codes: summary
(7, 4) codes
If there is at most 1 error, the code can correct the error if it exists.
If there are two errors, the code can detect but not correct the error.
Chapter 4 – Coding 80 / 85
Hamming Codes: summary
(8, 4) codes
The overall parity check can detect an odd number of errors.
Suppose it is known that the number of errors is at most 2.
If the overall parity check fails, then there is exactly one error and it can
be corrected.
If the overall parity passes and the parity check fails for at least one of the
circles, then there are two errors, but they cannot be corrected.
If all the parity checks pass, then there is no error.
Chapter 4 – Coding 81 / 85
Hamming (8, 4) Codes
Example 4.28
The following Hamming (8, 4) codeword is received:
00110001.
(b) Is it possible to detect the error and correct it if there is at most one
error?
Chapter 4 – Coding 82 / 85
Hamming (8, 4) Codes
(a) The overall parity check fails. So there must be odd number of errors.
After correcting the error, the original message (prior to encoding) should
be 0111.
Chapter 4 – Coding 84 / 85
Hamming (8, 4) Codes
Question
What advantage does Hamming (8, 4) codes have over Hamming (7, 4)
codes?
Chapter 4 – Coding 85 / 85