Professional Documents
Culture Documents
• History
• Calculation
• Using Hamming codes for single bit error correction
• data bits
• parity bits
Richard Hamming worked at Bell Labs in the 1940s on the Bell Model V
computer, an electromechanical relay-based machine with cycle times in
seconds. Input was fed in on punched cards, which would invariably have read
errors. During weekdays, special code would find errors and flash lights so the
operators could correct the problem. During after-hours periods and on
weekends, when there were no operators, the machine simply moved on to
the next job.
Hamming worked on weekends, and grew increasingly frustrated with having
to restart his programs from scratch due to the unreliability of the card reader.
Over the next few years, he worked on the problem of error-correction,
developing an increasingly powerful array of algorithms. In 1950, he published
Punch card (not actual size).
what is now known as Hamming Code, which remains in use today in
applications such as ECC memory.
Credit to Wikipedia
R. Jesse Chaney 3 CS271 – Oregon State University
Calculation
There is a lot of theory behind how the Hamming codes work. There are
numerous books and web pages that describe the theory.
I’m going to go very light on the theory and just jump into the calculation and
use of Hamming codes to correct single bit errors.
That aside, though there a large number of examples Hamming Code use on
the internet, I never found a step-by-step example that quite fit with how we
have described Hamming Codes in this class.
We are going to start with a number. We’ll use 8-bit numbers (0-255)
for this example. We could use any number, but I’ll select:
154
Next, we are going to convert the number 154 into its unsigned binary representation.
154 1 0 0 1 1 0 1 0
Wikipedia
We number the bits for the now 12 bits (8 for data and 4 for parity) from left to right starting
at 1. The parity bits are assigned to the locations in the 12 bits based on the powers of 2: 20
= 1, 21 = 2, 22 = 4, 23 = 8. If we had more than 8 bits of data, this would continue. Correctly
placing the parity bits within the 12 bits is very important.
Now we’ll calculate the value for the first parity bit, bit position 1.
Now we’ll calculate the value for the first parity bit, bit position 1. Count all the data bits
from the list. In the example, the indicated data bits count to 4 (positions 3, 7, 9, and 11).
If we are interested in even parity, the parity bit (position 1) is set to a zero. If we are
interested in odd parity, then the parity bit (position 1) is set to a one.
R. Jesse Chaney 8 CS271 – Oregon State University
Parity bit 2
Now we’ll calculate the value for the second parity bit, bit position 2.
Now we’ll calculate the value for the second parity bit, bit position 2. Count all the
data bits from the list. In the example, the indicated data bits count to 3 (positions 3,
7, and 11).
If we are interested in even parity, the parity bit (position 2) is set to a one. If we are
interested in odd parity, then the parity bit (position 2) is set to a zero.
R. Jesse Chaney 9 CS271 – Oregon State University
Parity bit 3
Now we’ll calculate the value for the third parity bit, bit position 4.
Now we’ll calculate the value for the third parity bit, bit position 4. Count all the data
bits from the list. In the example, the indicated data bits count to a lonely 1 (position 7).
If we are interested in even parity, the parity bit (position 4) is set to a one. If we are
interested in odd parity, then the parity bit (position 4) is set to a zero.
We now calculate the value for the fourth parity bit (last in our example), bit position 8.
Now we’ll calculate the value for the fourth parity bit (the last one in this example), bit
position 8. Count all the data bits from the list. In the example, the indicated data bits
count to 2 (positions 9 and 11).
If we are interested in even parity, the parity bit (position 8) is set to a zero. If we are
interested in odd parity, then the parity bit (position 8) is set to a one.
R. Jesse Chaney 11 CS271 – Oregon State University
Pulling all the parity bits together
Now we’ll put all the parity bits together.
_ _ 1 _ 0 0 1 _ 1 0 1 0
1 2 3 4 5 6 7 8 9 10 11 12
Data bits 0 1 0 0 1 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 1 0
_ _ 0 _ 1 0 0 _ 1 0 0 1 1 0 0 _ 1 0 1 1 0 0 0 0 0 0 0 1 0 1 1 _ 0 1 0 0 1 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
Parity bits
We are going to take our example from before, the number 154, and introduce an
error into the stream. We’ll do 2 examples. The first will have an error in the data bits.
The second will have an error in the parity bits. For both examples, we’ll use even
parity encoding.
It is important to know that Hamming codes can only correct single bit errors. There
are other more powerful error correcting codes, but they require more bits and are
more complex.
Though statistically, data errors are rare, you want to detect and correct when they do
occur and when they occur in clusters (such as memory or disk errors) you need to
detect and correct before you just have all garbage.
0 1 1 1 0 0 1 0 1 0 1 1 = 4 this is fine
Add all the values represented by the first parity bit, position 1. Count all the data bits
from the list. In the example, the indicated data bits count to 4 (positions 3, 7, 9, and
11). The value 4 is already even (for our even parity example). So, the single error
must not be within that group of bits.
0 1 1 1 0 0 1 0 1 0 1 1 = 4 this is fine
Add all the values represented by the second parity bit, position 2. Count all the data
bits from the list. In the example, the indicated data bits count to 4 (positions 2, 3, 7,
and 11). The value 4 is already even (for our even parity example). So, the single error
must not be within that group of bits.
0 1 1 1 0 0 1 0 1 0 1 1 = 3 this is wrong
Add all the values represented by the third parity bit, position 4. Count all the data
bits from the list. In the example, the indicated data bits count to 3 (positions 4, 7, and
12). The number 3 is not even (for our even parity example). So, one of the bits from
the parity bit 3 collection must be wrong.
0 1 1 1 0 0 1 0 1 0 1 1 = 3 this is wrong
Add all the values represented by the fourth parity bit, position 8. Count all the data
bits from the list. In the example, the indicated data bits count to 3 (positions 9, 11,
and 12). The number 3 is not even (for our even parity example). So, one of the bits
from the parity bit 4 collection must be wrong.
1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 1 0 0 1 0 1 0 1 1 = 3 this is wrong
0 1 1 1 0 0 1 0 1 0 1 1 = 3 this is wrong
Now that we’ve identified that there are 2 parity bits that are wrong (and which bits
Wrong parity bits
they are), how do we correct the error? We add the location of the wrong parity bits.
In this case: 4 + 8 = 12. It is bit 12 that is corrupted. So, instead of the received value
1, it should be a zero.
R. Jesse Chaney 20 CS271 – Oregon State University
Introduce an error in a parity bit
To introduce the error, we’ll just toggle bit 4 from zero to one.
Instead of corrupting a data bit, we’ll corrupt a parity bit for this example. We’ll
continue to use even parity.
0 1 1 0 0 0 1 0 1 0 1 0 Single error check 1 bit, skip 1 bit, check 1 bit, skip 1 bit
1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 0 0 0 1 0 1 0 1 0 = 4 this is fine
Add all the values represented by the first parity bit, position 1. Count all the data bits
from the list. In the example, the indicated data bits count to 4 (positions 3, 7, 9, and
11). The value 4 is already even (for our even parity example). So, the single error
must not be within that group of bits.
0 1 1 0 0 0 1 0 1 0 1 0 = 4 this is fine
Add all the values represented by the second parity bit, position 2. Count all the data
bits from the list. In the example, the indicated data bits count to 4 (positions 2, 3, 7,
and 11). The value 4 is already even (for our even parity example). So, the single error
must not be within that group of bits.
0 1 1 0 0 0 1 0 1 0 1 0 = 1 this is wrong
Add all the values represented by the third parity bit, position 4. Count all the data
bits from the list. In the example, the indicated data bits count to 3 (position 7). The
number 1 is not even (for our even parity example). So, one of the bits from the parity
bit 3 collection must be wrong.
0 1 1 0 0 0 1 0 1 0 1 0 = 2 this is fine
Add all the values represented by the fourth parity bit, position 8. Count all the data
bits from the list. In the example, the indicated data bits count to 2 (positions 9, 11).
The value 2 is already even (for our even parity example). So, the single error must not
be within this group of bits.
1 2 3 4 5 6 7 8 9 10 11 12
0 1 1 0 0 0 1 0 1 0 1 0 = 1 this is wrong
Now that we’ve identified that there is 1 parity bit that is wrong (and which bit it is),
Wrong parity bit
how do we fix the error? Like before, we add the location of the wrong parity bits. In
this example, it’s just 1 bit: 4. It is bit 4 that is corrupted. So, instead of the received
value zero, it should be a one.
R. Jesse Chaney 26 CS271 – Oregon State University
Conclusion