The Hamming Distance is the number of different symbols between two strings/numbers (equal length). It can be also considered as the number of changes required to convert from one input to another.
If the inputs are integers, we can write a quick implementation in Javascript like this:
const hamming_distance = (a, b) => {
let d = 0;
let h = a ^ b;
while (h > 0) {
d ++;
h &= h - 1;
}
return d;
}
The a ^ b calculates the XOR which is the difference in bit representation of two integers and h & (h – 1) removes the LSB (least significant bit). For example, 11100 & 11011 = 11000. 10001 & 10000 = 10000.
You can test the above JS code to compute the hamming distances between two integers in this online JS editor.
Hamming Weight / Hamming Distance Algorithms
Here are the posts related to Hamming Distance (XOR, The number of different bits):
- A faster approach to count set bits in a 32-bit integer
- Teaching Kids Programming – Minimum Bit Flips to Convert Number (Hamming Distance)
- GoLang: Compute the Hamming Distance
- How to Sort List by Hamming Weight in C++?
- Teaching Kids Programming – Compute the Hamming Distance of Two Integers
- Compute the Total Hamming Distance between All Pairs of Integers
- The Hamming Distance Implementation in Javascript
- Hamming Distance between Two Integers
- Compute Number of 1’s Bits in C/C++
- Teaching Kids Programming – Sort List by Hamming Weight
–EOF (The Ultimate Computing & Technology Blog) —
546 wordsLast Post: Introducing SteemIt Auto Claim Rewards
Next Post: SteemVBS Development - GetVotingPower, Get Post URL from Comment, Suggested Password, Effective SP, VestsToSP and more!

One line hamming distance in python:
def hamming_distance(s1, s2): return int(sum([c1==c2 for (c1, c2) in zip(s1, s2)]))