Hamming Error-Correction Codes: Allen D. Holliday February 17, 1994 (Revised June 15, 2002 March 1, 2004)
Hamming Error-Correction Codes: Allen D. Holliday February 17, 1994 (Revised June 15, 2002 March 1, 2004)
Allen D. Holliday
February 17, 1994 (revised June 15, 2002; March 1, 2004)
Introduction
Messages that are transmitted over a communication channel can be damaged; their bits can be
masked or inverted by noise. Detecting and correcting these errors is important. Some simple
codes can detect but not correct errors; others can detect and correct one or more errors. This
paper addresses one Hamming code that can correct a single-bit error and detect a double-bit
error. Other Hamming codes are more powerful, but beyond the scope of this paper.
Parity Checking
One simple way to detect errors is:
1. Count the number of ones in the binary message.
2. Append one more bit, called the parity bit, to the message
3. Set the parity bit to either 0 or 1, so that the number of ones in the result is even. For
example, if the original message contained 17 ones, the parity bit would be a one; if there
had been 16 ones, the parity bit would be a zero.
4. Count the number of ones in the received message, including the parity bit. The result
will always be even if no errors were encountered. (This approach also works if the parity
bit is set to make the count come out odd, as long as the receiver checks for an odd
count.)
This simple check does have two limitations: it only detects errors, without being able to correct
them; and it can’t detect errors that invert an even number of bits.
Data bits: 1 2 3 4 5 6 7 8
Message
bits: 1 2 3 4 5 6 7 8 9 10 11 12
Check Ones
group count
8 Parity bit
4 Parity bit
2 Parity bit
1 Parity bit
The table on the next page shows the assignment of each output message bit in messages up to
32 data bits long.
Each row contains:
• The output message bit’s number in decimal and binary.
• A check bit indicator and number, or a data bit indicator and number. Check bits are
those with only a single 1 in their binary bit number.
• Indicators of the check groups that the output message bit is a member of. There are X’s
in the columns that correspond to the Binary number weight columns with 1’s in them.
Output Message Bit Assignment Check Group Membership
Binary, with
bit position weights
Decimal 32 16 8 4 2 1 Check Data 1 2 4 8 16 32
1 0 0 0 0 0 1 C1 X
2 0 0 0 0 1 0 C2 X
3 0 0 0 0 1 1 D1 X X
4 0 0 0 1 0 0 C3 X
5 0 0 0 1 0 1 D2 X X
6 0 0 0 1 1 0 D3 X X
7 0 0 0 1 1 1 D4 X X X
8 0 0 1 0 0 0 C4 X
9 0 0 1 0 0 1 D5 X X
10 0 0 1 0 1 0 D6 X X
11 0 0 1 0 1 1 D7 X X X
12 0 0 1 1 0 0 D8 X X
13 0 0 1 1 0 1 D9 X X X
14 0 0 1 1 1 0 D10 X X X
15 0 0 1 1 1 1 D11 X X X X
16 0 1 0 0 0 0 C5 X
17 0 1 0 0 0 1 D12 X X
18 0 1 0 0 1 0 D13 X X
19 0 1 0 0 1 1 D14 X X X
20 0 1 0 1 0 0 D15 X X
21 0 1 0 1 0 1 D16 X X X
22 0 1 0 1 1 0 D17 X X X
23 0 1 0 1 1 1 D18 X X X X
24 0 1 1 0 0 0 D19 X X
25 0 1 1 0 0 1 D20 X X X
26 0 1 1 0 1 0 D21 X X X
27 0 1 1 0 1 1 D22 X X X X
28 0 1 1 1 0 0 D23 X X X
29 0 1 1 1 0 1 D24 X X X X
30 0 1 1 1 1 0 D25 X X X X
31 0 1 1 1 1 1 D26 X X X X X
32 1 0 0 0 0 0 C6 X
33 1 0 0 0 0 1 D27 X X
34 1 0 0 0 1 0 D28 X X
35 1 0 0 0 1 1 D29 X X X
36 1 0 0 1 0 0 D30 X X
37 1 0 0 1 0 1 D31 X X X
38 1 0 0 1 1 0 D32 X X X
Transmitting
Before transmission, even parity is established over each check bit and its associated data bits. In
the example below, the data bits have been copied into their proper positions in the output
message and the number of ones in each check group has been calculated. Check bits 2 and 8
have been set to 1 to make the parity count even; check bits 1 and 4 are 0, since their group’s
parity count is already even.
Data bits: 1 0 1 0 1 1 0 1
Message
bits: 0 1 1 0 0 1 0 1 1 1 0 1
Check Ones
group count
8 3 Parity bit
4 2 Parity bit
2 3 Parity bit
1 2 Parity bit
Receiving
When the transmitted message is received, a parity bit is calculated over each group of bits (data
and parity) and stored in a syndrome value from right to left. Parity for check bit 1 and its
associated bits are rightmost in the syndrome value, parity for check bit 2 and its associated bits
are to the left, then parity for check bit 4, and so forth. If the syndrome value is 0, there have
been no errors. For single bit errors, the syndrome value identifies the erroneous bit, which needs
to be inverted.
The following diagram, which uses the output message from the Transmitting example above,
shows this process. In this example, one bit in the output message—number 6, in the dark box—
has been inverted because of a transmission error. When the ones counts for the check groups are
calculated, groups 2 and 4 have odd counts, so a 1 is placed in their positions in the syndrome
word. The resulting syndrome value is 6, which is the erroneous bits number. Inverting that bit
restores it to its transmitted value. The data bits are extracted from the received message and can
be seen to be the original values.
Check Ones
group count
8 4 Syndrome bit
4 1 Syndrome bit
2 3 Syndrome bit
1 2 Syndrome bit
(Syndrome bit
is count LSB)
0 1 1 0 0 0 0 1 1 1 0 1 0 1 1 0
(Bit 6 is in error)
0 1 1 0 0 1 0 1 1 1 0 1
Data bits: 1 0 1 0 1 1 0 1
Extensions and Limitations
The preceding scheme handles only single-bit errors—two or more errors will cause an incorrect
syndrome value. To deal with this problem, another check bit can be added to establish even
parity over all other output bits, data and check. An even number of bit errors can now be
detected (but not corrected), resulting in these cases:
Syndrome Value Message Parity Result
Zero Even No error in message
Nonzero Odd Correctable single-bit error
Nonzero Even Uncorrectable even-number-of-bits error
Odd-number-of-bits errors beyond 1 bit (3, 5, 7,...) still won’t be handled correctly. The
syndrome value and the new parity check will both indicate an error, as in case 2. Since the
syndrome value can’t point to more than one erroneous bit, the correction made will be
insufficient.
Note, however, that single and double bit errors are handled properly.