Hamming Distance
Number of positions at which two strings differ.
Formula
Variables
Example
1011 vs 1001: differ in one bit → d = 1
How it works
Hamming distance counts how many symbol positions are different when two strings have the same length. The sum uses the indicator [x_i \ne y_i], which contributes 1 whenever the symbols at position i do not match and 0 when they do, so adding over all positions gives the total number of mismatches. This is useful because it turns a comparison problem into a simple count.
When to use it
Use Hamming distance whenever you need to measure how similar two equal-length strings, bit patterns, or codewords are. It is common in error detection/correction, digital communications, genetics, and comparing encoded data.
Worked example, step by step
Compare x = 110010 and y = 100110 Step 1: Align the symbols by position: 1 vs 1 → same, contributes 0 1 vs 0 → different, contributes 1 0 vs 0 → same, contributes 0 0 vs 1 → different, contributes 1 1 vs 1 → same, contributes 0 0 vs 0 → same, contributes 0 Step 2: Add the contributions: d(x,y) = 0 + 1 + 0 + 1 + 0 + 0 Step 3: Final result: d(x,y) = 2
Common mistakes to avoid
- Using strings of different lengths, which is not valid for standard Hamming distance
- Counting how many characters are present in one string but not the other instead of comparing positions
- Forgetting that each differing position counts as exactly 1, no matter which symbols appear there
Did You Know?
Richard Hamming devised his distance in 1950 after weekend computer crashes kept wrecking his jobs.
Share this formula
More in Cryptography & Security
View allModular Exponentiation
IntermediateCore operation of RSA encryption — raise a message to a power, modulo n.
RSA Decryption
AdvancedRecovers the message using the private exponent d.
Euler's Totient
IntermediateCounts integers below n coprime to it, for n = pq — used to make RSA keys.
Shannon Entropy
AdvancedAverage information content — and the theoretical strength of a random source.
Password Entropy
IntermediateBits of security from password length and character-set size.
Key Space Size
IntermediateNumber of possible keys for a b-bit key.