Hamming Distance Calculator
Find the Hamming distance between two binary strings, two numbers or two words of equal length, with the differing positions, XOR pattern and error detection and correction capability.
Hamming Distance
2
Differing Positions
3, 5
Similarity
71.43 % of 7 positions
XOR Pattern
0010100
If This Were the Minimum Distance
Detects 1, corrects 0 error(s)
Hamming Distance
Choose binary strings, decimal numbers or text, and enter two values of equal length. The calculator marks every differing position and gives the distance, similarity and what that distance means for error-correcting codes.
Formulas
- Distance d = number of positions i where ai ≠ bi = popcount(a XOR b)
- Detects dmin − 1 errors; corrects ⌊(dmin − 1)/2⌋
Codes and their minimum distance
| Code | dmin | Detects | Corrects |
|---|---|---|---|
| No coding | 1 | 0 | 0 |
| Single parity bit | 2 | 1 | 0 |
| Hamming(7,4) | 3 | 2 | 1 |
| Extended Hamming (8,4), SECDED | 4 | 3 | 1 |
| Triple repetition | 3 | 2 | 1 |
Worked examples
1. 1011101 and 1001001 (the defaults). Distance 2 at positions 3 and 5; XOR 0010100.
2. "karolin" and "kathrin". Distance 3.
3. 25 and 7. 11001 vs 00111: distance 4.
The detect/correct figures apply when this distance is the minimum between any two codewords of a code.
Related tools
- Checksums: CRC Calculator. XOR and bits: Binary Calculator.
Frequently Asked Questions
What is Hamming distance?
The number of positions at which two equal-length strings differ. 1011101 and 1001001 differ in positions 3 and 5, so the distance is 2.
How do I calculate it for binary numbers?
XOR the two values and count the 1 bits (popcount). 1011101 XOR 1001001 = 0010100, which has two 1s.
Can I use it for text?
Yes, character by character, if the strings have the same length: "karolin" and "kathrin" have distance 3.
What if the strings have different lengths?
Hamming distance is undefined. Use edit (Levenshtein) distance, which also counts insertions and deletions.
How does distance relate to error detection?
If a code's minimum distance between valid codewords is d, it can detect up to d − 1 bit errors and correct up to ⌊(d − 1)/2⌋.
What is the Hamming(7,4) code?
It adds 3 parity bits to 4 data bits; its minimum distance is 3, so it corrects any single-bit error or detects two.
What about a simple parity bit?
Adding one parity bit gives minimum distance 2: it detects single-bit errors but cannot correct them.
How are decimal numbers compared?
They are converted to binary and the shorter one is padded with leading zeros. 25 (11001) and 7 (00111) have distance 4.
Where is Hamming distance used?
Error-correcting codes in memory (ECC RAM) and communications, DNA sequence comparison, image hashing and nearest-neighbour search.
What is the similarity percentage?
The share of positions that match: (length − distance) ÷ length.
Related Calculators
CRC Calculator
Calculate Cyclic Redundancy Check (CRC) values using polynomial division. Enter data and generator polynomial to compute CRC checksum.
Binary Calculator
Convert between decimal, binary, octal, and hexadecimal number systems. Perform bitwise AND, OR, XOR, NOT, and shift operations.
Boolean Algebra Calculator
Evaluate Boolean logic gates (AND, OR, XOR, NAND, NOR) for up to 3 inputs. View truth table outputs and minterm expressions.
Encoding Calculator
Calculate string encoding sizes, byte representations, and storage requirements for different character encodings.
Floating Point Calculator
Convert a decimal number to its IEEE 754 floating point representation. View sign, exponent, and mantissa bits for single and double precision.
IP Subnet Calculator
Calculate subnet mask, network address, broadcast address, and number of usable hosts from an IP address and CIDR prefix length.
More in Computer Science · Engineering calculators · All calculators A–Z