A Hamming code calculator encodes data bits with parity bits so that a single flipped bit can be found and fixed. Enter your data, and you get the code word with the parity bits in their places. Paste in a word that you received, and the calculator works out the syndrome, finds the wrong bit and corrects it. It also gives the Hamming distance between two strings, which counts the places where they differ. It runs in your browser.
How to use the Hamming code calculator
- Choose encode, check a received word or distance.
- Enter the data bits, the received word or the two strings.
- Turn on the extended code if you want to detect two errors too.
- Press Calculate.
Where the parity bits go
The Wikipedia article on Hamming codes says that the bit positions that are powers of two, 1, 2, 4, 8 and so on, are parity bits, and that all the other positions hold data. Parity bit 1 covers the positions with the lowest bit set, which are 1, 3, 5, 7 and so on. Parity bit 2 covers 2 and 3, 6 and 7, 10 and 11, and so on, and parity bit 4 covers 4 to 7, 12 to 15, and so on. In general, the parity bit p covers every position i for which p AND i is not zero.[1] Each parity bit is chosen so that the group that it covers has an even number of ones. The page does not name the parity convention, but its example matches even parity, and the calculator uses it.[1]
A worked example
The page's example takes the data 10011010. The data bits are laid out in the positions that are not powers of two, which gives __1_001_1010, and the full code word is 011100101010.[1] You can check parity bit 1: the positions 3, 5, 7, 9 and 11 hold 1, 0, 1, 1 and 1, which add up to 4, an even number, so the parity bit is 0. The calculator gives the same word, the layout and the value of each parity bit: p1 = 0, p2 = 1, p4 = 1 and p8 = 0. A shorter example is the data 1011, which gives the 7-bit word 0110011. This one is worked from the page's rule.
How many parity bits
The page says that with m parity bits, the positions from 1 to 2^m − 1 can be covered, which leaves 2^m − m − 1 positions for data. Its list of codes gives (3,1), the repetition code, then (7,4), (15,11), (31,26), (63,57), (127,120), (255,247) and (511,502).[1] For any number of data bits, the calculator takes the smallest number of parity bits, r, for which 2^r is at least the data bits plus r plus 1. This follows from the page's count, and it gives a shortened code if the data does not fill the largest word. Thus 4 data bits need 3 parity bits and 8 data bits need 4.
Finding and fixing an error
To check a received word, the calculator adds up, by exclusive or, the positions of the bits that are 1. The result is the syndrome. If it is zero, no error was found. The page says that if the parity bits 1, 2 and 8 indicate an error, then the bit in position 1 + 2 + 8 = 11 is in error, and that if only one parity bit fails, that parity bit itself is the wrong one.[1] The calculator reads the syndrome as a position, flips that bit and gives the corrected word and the data bits. For the word 0110111 the syndrome is 101, which is position 5, and flipping it gives 0110011 and the data 1011.
The extended code
If you turn on the extended option, one more parity bit covers the whole word. The page says that the extended Hamming code has a minimum distance of 4, and so it can correct one error and detect two, which is called SECDED.[1] With the extra bit, a single error changes the overall parity, and the syndrome says where it is. Two errors leave the overall parity even but give a non-zero syndrome, and the calculator reports that they were detected and cannot be corrected. For the data 1011 the extended word is 01100110.
Hamming distance
The article on Hamming distance defines it as the number of positions at which the corresponding symbols of two strings of equal length are different. Its examples are that karolin and kathrin are 3 apart, karolin and kerstin are 3 apart, kathrin and kerstin are 4 apart, 0000 and 1111 are 4 apart, and 2173896 and 2233796 are 3 apart.[2] For binary strings, the distance is the number of ones in a XOR b.[2] The calculator shows the distance and the positions that differ, counting from 1.
What the distance means for a code
The page explains that a code with minimum distance d can detect up to d − 1 errors and correct up to the whole number part of (d − 1) ÷ 2. Hamming codes have a minimum distance of 3, so they correct one error, and the extended codes reach 4.[2] The calculator repeats this for the distance that you enter, so a distance of 3 detects 2 errors and corrects 1, and a distance of 5 corrects 2.
Common mistakes
- Counting positions from 0. Hamming positions start at 1.
- Putting data in parity positions. The positions 1, 2, 4, 8 are for parity.
- Expecting two errors to be fixed. A basic code fixes one, and the extended code only detects two.
- Comparing strings of different lengths. The distance needs equal lengths.
What next
Use this often? Add Hamming Code to your Home screen.
Frequently asked questions
What is a Hamming code calculator?
A tool that adds parity bits to data so a single bit error can be found and fixed, and finds the Hamming distance of two strings.
Where do the parity bits go in a Hamming code?
In the positions that are powers of two: 1, 2, 4, 8 and so on. Data bits fill the other positions.
How do I find which bit is wrong?
Add up the positions of the bits that are 1 using XOR. The result, the syndrome, is the position of the wrong bit, and 0 means no error.
What is the Hamming distance?
The number of positions at which two strings of equal length differ. karolin and kathrin are 3 apart.
How many errors can a Hamming code fix?
One. The minimum distance is 3, and the extended code with an overall parity bit, at 4, also detects two.
Was this tool helpful?
What went wrong?
We only receive the reason you pick, never what you typed into the tool.
Thanks for your feedback.