Skip to content

Instantly share code, notes, and snippets.

@crates
Created January 23, 2020 17:27
Show Gist options
  • Save crates/bd2a3b77a369760228c72a7d97570e8c to your computer and use it in GitHub Desktop.
Save crates/bd2a3b77a369760228c72a7d97570e8c to your computer and use it in GitHub Desktop.
Calculate the Hamming Distance between two integers (LeetCode Question)
/**
* @param {number} x
* @param {number} y
* @return {number}
*/
const hammingDistance = function(x, y) {
let distance = 0;
let xor = x ^ y; // bitwise operation calculates the xor
while (xor > 0) {
distance++;
xor &= xor - 1; // clear flags
}
return distance;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment