Parity Bit and Hamming Code
While data is being sent or stored, errors can happen where a 0 flips to a 1 or a 1 flips to a 0. A parity bit is a bit added to check whether such an error occurred, and a Hamming code goes one step further: it is a code designed so you can find which bit is wrong and fix it.
At a Glance
- Analogy: it is like putting a note in a delivery box that says "the number of items is even."
- If the receiver counts and gets an odd number, they know "something went wrong along the way." This is the parity bit.
- But they don't know which item went wrong.
- A Hamming code splits the items into several overlapping bundles and puts a note in each bundle. By combining which bundles' notes are wrong, you can pinpoint exactly the one item that went wrong.
Parity Bit
A bit added to check whether an error occurred while information was being transmitted
1 bit is added to each character (bundle of bits) of the data to be transmitted.
Types: Even Parity and Odd Parity
The parity bit is chosen so that the number of 1s across all bits is even or odd.
| Type | Rule |
|---|---|
| Even parity | Makes the total number of 1s, including the parity bit, even |
| Odd parity | Makes the total number of 1s, including the parity bit, odd |
Example: With Even Parity, What If the 7-bit Data Is 1010001?
1010001has 3 1s in total (odd).- To make it even, one more 1 is needed, so the parity bit is 1.
- Attaching the parity bit at the front, the answer is
11010001.
def even_parity_bit(bits):
# If the number of 1s is odd, attach a 1 to make the total even
return '1' if bits.count('1') % 2 == 1 else '0'
data = '1010001'
p = even_parity_bit(data)
print(p + data) # result with the parity bit attached at the front
received = '11000001' # case where 1 bit flipped during transmission
print(received.count('1') % 2 == 0) # even means OK, False means an error was found11010001
FalseLimits of the Parity Bit
- You only know that there is an error, not which bit is wrong. So it can't be fixed, and you have to ask for it to be sent again.
- If two bits flip at the same time, the number of 1s becomes even again and the error goes undetected. It only catches errors when an odd number of bits are wrong.
Hamming Code
A self-correcting code that can fix a 1-bit error on its own during data transmission
Since a parity bit only detects errors and can't fix them, the Hamming code is used. By looking at several parity bits, it can find and fix the position where a 1-bit error occurred.
Method
- Number the positions from the left: 1, 2, 3, ...
- The positions that are powers of 2, that is, positions 1, 2, and 4, are parity bits. The 4 data bits go into the remaining positions 3, 5, 6, and 7. (Based on a 7-bit Hamming code)
- Each parity bit is responsible for an overlapping bundle starting from its own position, and checks whether the number of 1s in the bundle is even (or odd).
| Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| Role | P1 | P2 | Data | P4 | Data | Data | Data |
| P1 bundle (positions 1, 3, 5, 7) | O | O | O | O | |||
| P2 bundle (positions 2, 3, 6, 7) | O | O | O | O | |||
| P4 bundle (positions 4, 5, 6, 7) | O | O | O | O |
The rule for forming bundles shows up when you write the position numbers in binary. Bundle 1 is the positions whose last binary digit is 1 (1, 3, 5, 7), bundle 2 is the positions whose second-to-last digit is 1 (2, 3, 6, 7), and bundle 4 is the positions whose third-to-last digit is 1 (4, 5, 6, 7). So each position from 1 to 7 belongs to a different combination of bundles, and just by looking at the combination of wrong bundles, you can tell which position is wrong.
This relationship becomes clear at a glance in a picture of three overlapping circles. Each circle is the bundle of one parity bit, and the data bits d1 to d4 each sit where a different combination of circles overlaps. In the picture, p1, p2, p3 are P1, P2, P4 from the table above, and d1, d2, d3, d4 are positions 3, 5, 6, 7. For example, if d4 (position 7), where all three circles overlap, is wrong, all three bundles are wrong, and if a position that belongs only to p1 is wrong, only the p1 bundle is wrong.

Image source: Wikimedia Commons (CC BY-SA 3.0)
Example: If an Even-Parity Hamming Code Is 0011011, What Is the Corrected Code?
Position 1 2 3 4 5 6 7
Received 0 0 1 1 0 1 1- Check bits 1, 3, 5, 7:
0 1 0 1, two 1s, which is even, so 0 (no problem) - Check bits 2, 3, 6, 7:
0 1 1 1, three 1s, which is odd, so 1 (problem) - Check bits 4, 5, 6, 7:
1 0 1 1, three 1s, which is odd, so 1 (problem)
Reading the three results in reverse order (bundle 4, bundle 2, bundle 1) gives 110. Binary 110 is 6 in decimal, so the 6th bit is wrong.
Changing the 6th bit from 1 to 0, the answer is 0011001.
A check result of 000 means there is no error.
Checking in Code
def hamming_encode(d):
# d: a 4-bit data string (goes into positions 3, 5, 6, 7)
b = [0] * 8 # leave index 0 empty so we can start from 1
b[3], b[5], b[6], b[7] = (int(c) for c in d)
b[1] = (b[3] + b[5] + b[7]) % 2 # make bundle 1, 3, 5, 7 even
b[2] = (b[3] + b[6] + b[7]) % 2 # make bundle 2, 3, 6, 7 even
b[4] = (b[5] + b[6] + b[7]) % 2 # make bundle 4, 5, 6, 7 even
return ''.join(map(str, b[1:]))
def hamming_check(code):
b = [0] + [int(c) for c in code]
c1 = (b[1] + b[3] + b[5] + b[7]) % 2
c2 = (b[2] + b[3] + b[6] + b[7]) % 2
c4 = (b[4] + b[5] + b[6] + b[7]) % 2
pos = c4 * 4 + c2 * 2 + c1 # binary read in reverse = wrong position
if pos:
b[pos] ^= 1 # flip the bit at that position to fix it
return f'{c4}{c2}{c1}', pos, ''.join(map(str, b[1:]))
print(hamming_check('0011011'))
print(hamming_encode('1001'))
print(hamming_check('0011001'))('110', 6, '0011001')
0011001
('000', 0, '0011001')- First line: checking the example's
0011011gives110, an error at position 6, and the fixed result is0011001. - Second line: turning the data
1001into a Hamming code gives exactly0011001. This means the example's original data was1001. - Third line: checking the fixed code again gives
000, no error.
Limits of the Hamming Code and Real-World Use
- A 7-bit Hamming code can fix only 1-bit errors. If 2 bits are wrong at the same time, it may "fix" the wrong position.
- So in practice, one more overall parity bit is often added in a scheme that fixes 1-bit errors and only detects 2-bit errors (SECDED).
- Server ECC memory uses this principle to automatically fix bit errors that occur in RAM. For memory itself, see Computer Components and Memory Management.
Summary
| Concept | Plain-language summary |
|---|---|
| Parity bit | A single check bit added to make the number of 1s even (or odd) |
| Even / odd parity | Whether the total number of 1s is made even or odd |
| Limits of parity | You only know something is wrong, not where. It can't catch 2 wrong bits |
| Hamming code | Puts a parity bit on each overlapping bundle to find and fix the position of a single wrong bit |
| Parity positions | Positions that are powers of 2, like 1, 2, 4, 8 |
| Finding the error position | The binary number from reading the per-bundle check results in reverse is the wrong position number |
| ECC memory | Memory that automatically fixes bit errors in RAM using the Hamming code principle |
