You are on page 1of 7

Hamming Error-Correction Codes

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 cant detect errors that invert an even number of bits. Hamming Code Approach Hamming codes are an extension of this simple method that can used to detect and correct a larger set of errors. The basic idea is to have several parity bits (called check bits in Hamming codes) and assign different bits to several overlapping groups. If some parity bits are correct and others are not, the bit in error can be deduced. A very simple example may help to understand this approach. Suppose that the original message is four bits long, numbered from 1 to 4. Then add three check bits as follows: Check bit 1 establishes even parity over itself and data bits 1 and 2 (the first two) Check bit 2 establishes even parity over itself and data bits 1 and 3 (the odd-numbered bits) Check bit 3 establishes even parity over itself and data bits 2 and 4 (the odd-numbered bits)

Assuming that only one bit can be in error, it can be determined with the following table: Check bits in error None 1 and 2 1 and 3 2 only 3 only Data bit in error None 1 (odd-numbered bit of the first two) 2 (even-numbered bit of the first two) 3 (odd-numbered bit of the second two) 4 (even-numbered bit of the second two)

Although this code works, its fairly inefficient; there are almost as many check bits as data bits.

Hamming Single-bit Correction Code The Hamming single-bit correction code is implemented by adding check bits to the output message according to the following pattern: 1) The message bits are numbered from left to right, starting at 1. 2) Every bit whose number is a power of 2 (bits 1, 2, 4, 8,) is a check bit. 3) The other output message bits (bits 3, 5, 6, 7, 9,) contain the data bits, in order. Each check bit establishes even parity over itself and a group of data bits. A data bit is in a check bits group if the binary representation of the data bits number contains a 1 in the position of the check bits weight. For instance, the data bits associated with check bit 2 are all those with a 1 in the 2s position of their binary bit numberbits 2, 3, 6, 7, and so forth. The following diagram, which is for an 8-bit data message, illustrates this. It shows the assignment of data bits and check bits to the bits of a12-bit output message. The horizontal lines below the output message box have dots to indicate the assignment of output bits to check groups. Each check group has a ones count box, with a parity bit that is inserted into the output message.
Data bits:

Message bits:

9 10 11 12

Check Ones group count 8 4 2 1 Parity bit Parity bit Parity bit Parity bit (Parity bit makes count even)

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 bits 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 Xs in the columns that correspond to the Binary number weight columns with 1s in them.
Output Message Bit Binary, with bit position weights Decimal 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 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 4 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 2 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 Assignment Check Group Membership

Check C1 C2

Data

1 X

2 X X

16

32

D1 C3 D2 D3 D4 C4 D5 D6 D7 D8 D9 D10 D11 C5 D12 D13 D14 D15 D16 D17 D18 D19 D20 D21 D22 D23 D24 D25 D26 C6 D27 D28 D29 D30 D31 D32

X X X X X X X X X X X X X X X X X X

X X

X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X

X X X X X X

X X

X X X X X X X X X X X X X X

X X

X X X X X X

X X

X X 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 groups parity count is already even.

Data bits:

Message bits:

Check Ones group count 8 4 2 1 3 2 3 2 Parity bit Parity bit Parity bit Parity bit (Parity bit makes count even)

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 messagenumber 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 2 1 4 1 3 2 Syndrome bit Syndrome bit Syndrome bit Syndrome bit (Syndrome bit is count LSB)

(Bit 6 is in error)

Data bits:

Extensions and Limitations The preceding scheme handles only single-bit errorstwo 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 Zero Nonzero Nonzero Message Parity Result Even Odd Even No error in message Correctable single-bit error Uncorrectable even-number-of-bits error

Odd-number-of-bits errors beyond 1 bit (3, 5, 7,...) still wont be handled correctly. The syndrome value and the new parity check will both indicate an error, as in case 2. Since the syndrome value cant point to more than one erroneous bit, the correction made will be insufficient. Note, however, that single and double bit errors are handled properly.

You might also like