Lecture-10
Memory-6
Error Detection and Correction
CSE-2823
Computer Architecture
Dr. Md. Waliur Rahman Miah
Associate Professor, CSE, DUET
1
Today’s Topic
Error
Detection and Correction
Ref:
Hennessy-Patterson 5e-Ch-5; 4e-Ch-7
Stallings 8e-Ch-4-5-6
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 2
Error Detection Code
• A code that enables the detection of an error in data, but
not correction of the error.
• It cannot find out the precise location and, hence, cannot
correct the error.
• e.g.: Parity code
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET
Hamming Error Correction Code
• a parity code cannot correct errors, it only detects errors
• Hamming code is useful for both detection and correction of
error present in the received/stored data.
• This code uses multiple parity bits and we have to place these
parity bits in the positions of powers of 2.
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 4
The Hamming Code
• Hamming uses multiple parity bits for error detection.
• In a parity code, the number of 1s in a word is counted;
• In odd parity the number of 1s is odd including the parity bit.
• In even parity the number of 1s is even including the parity bit.
• When a word is written into memory, the parity bit is also written.
• Then, when the word is read out, the parity bit is read and
checked.
• If the computed parity of the memory word and the stored parity
bit do not match, an error has occurred.
• Hamming distance is used to find the limits in the number of bits
in error detection and correction.
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 5
Hamming distance
• Hamming distance is the minimum number of bits that are different between any
two correct bit patterns.
– For example, the distance between 011011 and 001111 is two.
• What happens if the minimum distance between members of a codes is two, and
we get a one-bit error?
• It will turn a valid pattern in a code to an invalid one.
• Thus, if we can detect whether members of a code are valid or not,
• we can detect single bit errors, and can say we have a single bit error detection
code.
• If there are 2 bits of error, then a 1-bit parity scheme will not detect any error,
since the parity will match the data with two errors.
• a 1-bit parity code is limited to detecting a single bit of error.
• In particular, a code C is said to be k error detecting if, and only if, the minimum
Hamming distance between any two of its codewords is at least k+1.
• A code is k-errors correcting if, and only if, the minimum Hamming distance
between any two of its codewords is at least 2k+1.
• Thus a code with minimum Hamming distance d between its codewords can detect
at most d-1 errors and can correct ⌊(d-1)/2⌋ errors.
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 6
Hamming Error Correction Code
• Hamming code uses multiple parity bits
• We can find the required number of parity bits by using the
following mathematical relation:
2k ≥ n + k + 1
• Where:
• ‘n’ is the number of bits in the binary code (information)
• ‘k’ is the number of parity bits
• The number of bits in the Hamming code (data + parity) is
equal to n + k.
• If number of bits in data is n = 8; then according to the above
relation: number of parity bits required is k = 4
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 7
Hamming ECC steps
1. Start numbering bits from 1 on the left , as opposed to the traditional
numbering of the rightmost bit being 0.
2. Mark all bit positions that are powers of 2 as parity bits
(positions 1, 2, 4, 8, 16, …) .
3. All other bit positions are used for data bits
(positions 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, …).
4. The position of parity bit determines sequence of data bits that it checks:
(Figure 5.24 next slide shows this coverage graphically)
■ Bit 1 (0001two) checks bits (1,3,5,7,9,11,...), which are bits where rightmost bit of
address is 1 (0001two, 0011two, 0101two, 0111two, 1001two, 1011two,…).
■ Bit 2 (0010two) checks bits (2,3,6,7,10,11,14,15,…), which are the bits where the
second bit to the right in the address is 1.
■ Bit 4 (0100two) checks bits (4–7, 12–15, 20–23,...) , which are the bits where the
third bit to the right in the address is 1.
■ Bit 8 (1000two) checks bits (8–15, 24–31, 40–47,...), which are the bits where the
fourth bit to the right in the address is 1.
Note that each data bit is covered by two or more parity bits.
5. Set parity bits to create even parity for each group.
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 8
Hamming ECC Parity bit coverage
FIGURE 5.24 Parity bits, data bits, and field coverage in a
Hamming ECC code for eight data bits.
P1 => hop 1 by 1; P2 => hop 2 by 2; P4 => hop 4 by 4; P8 => hop 8 by 8;
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 9
Hamming ECC detection & correction
• you can determine whether bits are incorrect by looking at the parity bits.
• Using the 12 (8 data + 4 parity) bit code in Figure 5.24 (prev slide):
• Way-1:
– if the value of all four parity bits p1,p2,p4,p8 are equal to the computed parity bits,
then there was no error.
– if the value of parity bits p1,p2,p4,p8 are not equal to the computed parity bits,
then there was an error. Say, p2 and p8 are incorrect then 2+8 = 10ten ; bit 10 (d6) is
an error.
• Way-2:
– create a new bit pattern as p8p4p2p1 and set bits for incorrect parities
– if the value of all the four parity bits are correct then the pattern becomes
p8p4p2p1 = 0000, and there is no error.
– if the value of some parity bits are incorrect then we set 1 for incorrect parity. Say,
p8 and p2 are incorrect then set 1 for p8 and p2; the pattern p8p4p2p1 = 1010, and
there is error at position 1010two, which is 10ten, then bit 10 (d6) is an error.
– Since the number is binary, we can correct the error just by inverting the value of
error bit pos (which is bit pos 10 in the example).
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 10
Hamming ECC Example
• a) Assume one byte data value is 10011010two. Find out the
Hamming ECC code for that byte.
• b) Due to some error, bit 10ten is inverted; show that the ECC
code finds and corrects the single bit error.
• data value is: 10011010two
• The final code word is:
011100101010
• Inverting bit 10ten ; code with an error bit:
011100101110
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 11
Hamming ECC Example contd…
• Leaving spaces for the parity bits, the 12 bit pattern is:
__1_001_1010
• Position 1 checks bits 1,3,5,7,9, and 11, which we highlight:
__1_001_1010
• To make the group even parity, we should set bit 1 to 0 :
0_1_001_1010
• Position 2 checks bits 2,3, 6,7, 10,11, which is:
0 _ 1 _ 0 0 1 _ 1 0 1 0 ; odd 1’s, so we set position 2 to a 1:
011_001_1010
• Position 4 checks bits 4,5,6,7, 12, which is:
0 1 1 _ 0 0 1 _ 1 0 1 0 ; odd 1’s, so we set bit 4 to a 1 :
0111001_1010
• Position 8 checks bits 8,9,10,11,12, which is
0 1 1 1 0 0 1 _ 1 0 1 0 ; even 1’s, so we set bit 8 to a 0:
0 1 1 1 0 0 1 0 1 0 10 which is the final code word
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 12
Hamming ECC Example contd…
• The final code word is 011100101010.
Bit pos 1 2 3 4 5 6 7 8 9 10 11 12
Encoded bits p1 p2 d1 p4 d2 d3 d4 p8 d5 d6 d7 d8
0 1 1 1 0 0 1 0 1 0 1 0
• We check if Hamming ECC finds and corrects a single bit error.
• Due to some error, bit 10ten inverted; code word become:
011100101110.
Bit pos 1 2 3 4 5 6 7 8 9 10 11 12
Encoded bits p1 p2 d1 p4 d2 d3 d4 p8 d5 d6 d7 d8
0 1 1 1 0 0 1 0 1 1 1 0
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 13
Hamming ECC Example contd…
• Due to some error, code word become: 011100101110.
Bit pos 1 2 3 4 5 6 7 8 9 10 11 12
Encoded bits p1 p2 d1 p4 d2 d3 d4 p8 d5 d6 d7 d8
0 1 1 1 0 0 1 0 1 1 1 0
• Parity bit p1 is 0 ( 0 1 1 1 0 0 1 0 1 1 1 0)
– Numbers of 1’s in p1d1d2d4d5d7 is four 1s, so even parity; this group is OK).
• Parity bit p2 is 1 (0 11 10 01 01 11 0)
– p2d1d3d4d6d7 contains five 1s, so odd parity; there is an error somewhere).
• Parity bit p4 is 1 (011 1001 0111 0)
– p4d2d3d4d8 has two 1s, so even parity; this group is OK).
• Parity bit p8 is 0 (0111001 01110)
– p8d5d6d7d8 is three 1s, so odd parity; there is an error somewhere).
• Parity bits p2 and p8 are incorrect. As 2 + 8 = 10, bit 10 must be wrong.
• Another way: create a new bit pattern as p8p4p2p1. set bits for incorrect
parities => 1010two => 10ten => bit pos 10 (d6) is error
• The error can be corrected simply by inverting bit 10: 011100101010.
• Hence, Hamming ECC can find the position of error and correct it.
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 14
Reference
[1] Patterson, D. A., & Hennessy, J. L. (2014). Computer organization
and design: The hardware/software interface (5th ed.). Burlington,
MA: Morgan Kaufmann Publishers.
[2] William Stallings, (2010), Computer Organization and
Architecture,(8th Ed), Prentice Hall Upper Saddle River, NJ 07458.
[3] https://www.tutorialspoint.com/digital_circuits/digital_circuits
_error_detection_correction_codes.htm
[4] https://en.m.wikipedia.org/wiki/Hamming_distance
Dr. Md. Waliur Rahman Miah Dept of CSE, DUET 15