The Hamming distance counts how many bit positions differ between two values. It is the foundation of error-correcting codes, perceptual-hash comparison, and sequence analysis. This tool computes it for any two non-negative integers, entered in binary, decimal, or hex, and shows exactly where the bits disagree.
How it works
Computing the Hamming distance reduces to two well-known bit operations:
diff = a XOR b // 1 wherever the bits differ
distance = popcount(diff) // count those 1 bits
XOR yields a 1 in every position where the two inputs disagree and a 0 where they match, so the population count of the XOR is precisely the number of differing positions. The tool left-pads both numbers to the same bit width before displaying them, matching the convention that the shorter value carries implicit leading zeros.
Worked example
Comparing the binary values 10110110 and 11100100:
10110110
11100100
--------
XOR 01010010
The XOR result 01010010 has three 1 bits at positions 1, 4, and 6 (counting from the right). The Hamming distance is therefore 3.
In decimal, that is comparing 182 and 228 — their XOR is 82, and the popcount of 82 (binary 01010010) is 3.
Practical uses
Error-correcting codes: The fundamental idea in codes like Hamming(7,4) and BCH codes is to ensure that any two valid codewords differ in at least a minimum number of bit positions. A minimum distance of 3 allows single-bit error detection and correction: if you receive a word, the nearest valid codeword (the one with the smallest Hamming distance) is the most likely intended value. Distance 4 allows detecting two-bit errors.
Perceptual image hashing: A perceptual hash (pHash, dHash) turns an image into a 64-bit integer that changes smoothly as the image changes. Two hashes with a Hamming distance of 0 are identical images; a distance below about 10 typically indicates the same image with minor edits, resizing, or compression artefacts. Distance above 20–30 usually means different images.
DNA and protein sequences: Mutations in a fixed-length sequence are exactly bit-position changes if you encode the bases as bits. Hamming distance counts the number of substitution mutations between two sequences of the same length (but not insertions or deletions, which require edit distance instead).
Network IP addressing: Comparing subnet masks or checking prefix lengths sometimes involves counting differing bits, though this is more commonly framed as bitwise AND operations.
Example and notes
Comparing 10110110 and 11100100, the XOR is 01010010, which has three 1
bits — so the Hamming distance is 3. In coding theory the minimum Hamming
distance of a code sets its strength: a distance of d lets you detect up to
d − 1 errors and correct up to (d − 1) / 2. The same measure compares image
hashes (a small distance means visually similar) and counts mutations between
biological sequences. This calculator uses BigInt, so the values can be
arbitrarily large.