What is Hamming Distance?#
Hamming Distance is the number of differing characters between two strings of equal lengths.
- "Apple" and "Apolo": 2 characters
- "Cat" and "Kat": 1 character
- "Dog" and "Dog": 0 characters
Hamming Distance between integers#
What is the Hamming Distance between
9? Are they both
1? That can't be so, right?
Hamming Distance between integers is calculated at the bit level.
The above implemenation can be further optimized by the use of bitwise operators
^ (Bitwise XOR),
& (Bitwise AND) and
>> (Sign-propagating right shift).
This implementation is significantly faster than the first one, since it works at the bit level and avoids string operations.
In some domains, like Cryptography, it may be required to calculate the bit-level Hamming Distance between two strings. It is accomplished by converted the strings to their respective numeric ASCII codes and then running the integral Hamming Distance calculation on them.
Looking for a handy online Hamming Distance calculator? There is one at /tools/calculators/hamming-distance.html.
Hamming Distance is the number of differing characters between two strings of equal lengths or the number of differing bits between two numbers.