Skip to content

Instantly share code, notes, and snippets.

@VojtaStavik
Created April 5, 2017 19:18
Show Gist options
  • Save VojtaStavik/bc68f1ef55872ad47169f7702c9efa93 to your computer and use it in GitHub Desktop.
Save VojtaStavik/bc68f1ef55872ad47169f7702c9efa93 to your computer and use it in GitHub Desktop.
+ (NSInteger) calculateHammingDistanceFor:(NSInteger)x and:(NSInteger)y {
// Step 1: Find different bits
NSUInteger differentBits = x ^ y;
// Step 2: Count them
NSUInteger counter = 0;
while (differentBits > 0) {
NSUInteger maskedBits = differentBits & 1;
if (maskedBits != 0) {
counter++;
}
differentBits = differentBits >> 1;
}
return counter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment