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.

Built by Sabki SolutionsRules: d = popcount(a XOR b)Last updated
1011101
1001001
··^·^··

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

CodedminDetectsCorrects
No coding100
Single parity bit210
Hamming(7,4)321
Extended Hamming (8,4), SECDED431
Triple repetition321

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

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

More in Computer Science · Engineering calculators · All calculators A–Z