How this instrument works
The Hamming distance between two equal-length strings of symbols is simply the number of positions at which the two strings differ. For binary strings, that means comparing each bit position one at a time and counting how many positions have a 0 in one string and a 1 in the other. Two identical strings have a Hamming distance of 0; two strings that disagree in every position have the maximum possible distance, equal to the string length.
The concept was introduced by Richard Hamming in his 1950 paper 'Error Detecting and Error Correcting Codes,' published in the Bell System Technical Journal, as part of the foundational work on error-correcting codes at Bell Labs. Hamming distance measures how many single-bit errors would need to occur to turn one string into the other — which is exactly why it underlies error-correcting code design: codes are built so that all valid codewords sit at a large minimum Hamming distance from each other, so a small number of bit flips can never accidentally turn one valid codeword into another.
Beyond its origins in telecommunications, Hamming distance shows up anywhere two fixed-length sequences need to be compared position by position: measuring bit-error rate in a noisy digital channel, comparing DNA or protein sequences of equal length in bioinformatics, and clustering or nearest-neighbour search over binary feature vectors (like perceptual image hashes) in machine learning.
This instrument compares two 8-bit strings, letting you toggle each bit of string A and string B independently and see the running distance update — a hands-on way to see exactly which positions are driving the count up or down.
- Toggle Bit 1 through Bit 8 under string A to set your first 8-bit value.
- Toggle Bit 1 through Bit 8 under string B to set your second 8-bit value.
- Read Hamming distance for the count of bit positions where A and B disagree.
- Set A and B identical to confirm the distance reads 0, then flip a single bit in either string to see the distance increase by exactly 1 per flip.
- Use this to sanity-check error-correcting-code minimum distances, or to compare two received bit patterns for likely transmission errors.
Worked example — comparing A = 10110000 and B = 11100000
Set string A to 1,0,1,1,0,0,0,0 and string B to 1,1,1,0,0,0,0,0. Hamming distance reads 2. Comparing position by position: bit 1 matches (1=1), bit 2 differs (0 vs 1), bit 3 matches (1=1), bit 4 differs (1 vs 0), and bits 5 through 8 all match (0=0 throughout).
Exactly two positions disagree — positions 2 and 4 — so the Hamming distance is 2. In error-correction terms, this means string A could be turned into string B (or vice versa) by flipping exactly two bits, no fewer; a code designed to correct single-bit errors would need every pair of valid codewords to sit at a Hamming distance of at least 3 to guarantee this particular pair couldn't be confused for each other after just one bit flip.
Questions
What does a Hamming distance of 0 mean?
A Hamming distance of 0 means the two strings are completely identical — every bit position matches. It's the minimum possible Hamming distance for any pair of strings, and the only way to achieve it is for the two strings to be exactly the same, position for position.
How is Hamming distance used in error-correcting codes?
Error-correcting codes work by choosing a set of valid 'codewords' that are all spaced apart by a guaranteed minimum Hamming distance. If the minimum distance between any two valid codewords is d, the code can detect up to d-1 bit errors and correct up to floor((d-1)/2) bit errors, because a received string with fewer errors than that stays closer to the original codeword than to any other valid one. This is the core design principle behind schemes like Hamming codes, Reed-Solomon codes, and many others used in memory, storage and communications hardware.
Can Hamming distance be used on strings that aren't the same length?
No — Hamming distance is only defined for two strings of equal length, since it works by comparing corresponding positions one at a time. If the strings have different lengths, there's no well-defined position-by-position comparison to make. Comparing sequences of different lengths instead calls for a different measure, such as edit distance (Levenshtein distance), which allows insertions and deletions as well as substitutions.
What's the maximum possible Hamming distance for two 8-bit strings?
The maximum is 8, achieved when the two strings disagree at every single position — for example, all-ones (11111111) compared against all-zeros (00000000). Each of the 8 bit positions contributes at most 1 to the total distance, so 8 positions can contribute at most 8 total, which happens exactly when the two strings are bitwise complements of each other.
How is Hamming distance different from Hamming weight?
Hamming weight is the number of 1-bits within a single string (equivalently, its Hamming distance from the all-zero string). Hamming distance, by contrast, always compares two strings against each other and counts the positions where they differ. The two concepts are closely related — the Hamming distance between any two strings equals the Hamming weight of their bitwise XOR — but weight describes one string in isolation, while distance always describes a relationship between two.