Hamming Distance Calculator for Binary Words – Free

Enter two binary words of the same length and this Hamming distance calculator counts the positions where they differ. For 101101 and 100001 the distance is 2.

How to use the Hamming distance Calculator tool

  1. Enter the first bit string in First binary word, for example 101101.
  2. Enter a string of the same length in Second binary word, for example 100001.
  3. Select Calculate to compare the two words.
  4. Read the distance, a single whole number, in the Result box.
  5. Select Copy to copy the number, or Example to rerun the sample pair.

What Hamming distance measures

The Hamming distance between two strings of equal length is the number of positions at which their symbols differ. For binary words it is the number of bit flips needed to turn one word into the other. Richard Hamming introduced the measure in his 1950 paper on error-detecting and error-correcting codes.

The tool computes it the way hardware and software usually do: XOR the two words, which leaves a 1 in every differing column, then count the ones in the result.

Hamming distance example

The words disagree in bits 3 and 2, counted from the right starting at 0, so the distance is 2. Identical words give 0, and a word compared with its bitwise inverse gives the full length: 1010 1010 against 0101 0101 returns 8.

First word    101101
Second word   100001
XOR           001100
Distance      2

Equal lengths are required

Hamming distance is undefined for strings of different lengths, and the tool enforces that. If the two inputs differ in length after cleanup, you get the message Enter two binary words of equal length. Nothing is padded automatically, so 00000101 and 101 are rejected even though they are the same number. Add leading zeros to the shorter word yourself.

Each field takes 1 to 65,536 binary digits. Spaces and a leading 0b are stripped before the lengths are compared. Only 0 and 1 are accepted, so this calculator does not compare text strings or DNA sequences. The comparison is done on your device, and neither word leaves the page.

Why minimum distance matters in error correction

The strength of a code is set by its minimum distance d, the smallest Hamming distance between any two valid code words. Such a code can detect up to d - 1 bit errors and correct up to (d - 1) / 2 of them, rounded down. A single parity bit gives d = 2, enough to detect one error. The Hamming(7,4) code has d = 3 and corrects one error. Extended Hamming codes with d = 4, used in ECC memory, correct one error and detect two.

The measure is also used to compare perceptual image hashes and SimHash fingerprints, where a small distance means near-duplicate content.

Frequently asked questions

What is the Hamming distance between 101101 and 100001?

The distance is 2. The words differ in the third and fourth positions from the left and match everywhere else.

How do I calculate Hamming distance in code?

XOR the two integers and count the set bits. In Python that is bin(a ^ b).count('1'), or (a ^ b).bit_count() from version 3.10. In C use __builtin_popcount(a ^ b). The result matches this tool when both values share a width.

Can I compare binary words of different lengths?

Not directly. The calculator returns an error when the lengths differ. Pad the shorter word with leading zeros to the same width first, if that matches how your data is laid out.

What is the difference between Hamming distance and Hamming weight?

Hamming weight is the number of ones in a single word. Hamming distance compares two words. They are linked: the distance between A and B equals the weight of A XOR B, and the weight of a word is its distance from all zeros.

What is the Hamming distance between ASCII A and a?

It is 1. Uppercase A is 01000001 and lowercase a is 01100001. They differ only in bit 5, the bit that separates the two letter cases in ASCII.

Is Hamming distance the same as edit distance?

No. Hamming distance only counts substitutions at fixed positions and needs equal lengths. Edit distance, or Levenshtein distance, also allows insertions and deletions, so it can compare strings of different lengths.

Related tools