Skip to content

Instantly share code, notes, and snippets.

@levidurfee
Created February 2, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levidurfee/66759a23ecd0020be1104bc9c558793a to your computer and use it in GitHub Desktop.
Save levidurfee/66759a23ecd0020be1104bc9c558793a to your computer and use it in GitHub Desktop.
Hamming distance in C
#include "time.h"
#include "stdio.h"
int hamming_distance(long int n, long int m);
int main()
{
printf("%d [should be 3]\n", hamming_distance(0x3c3e0e1a3a1e1e1e, 0x3c3e0e3e3e1e1e1e));
printf("%d [should be 6]\n", hamming_distance(55, 64));
printf("%d [should be 32]\n", hamming_distance(0x69684858535b7575, 0xe1e1e2a7bbaf6faf));
printf("%d [should be 119]\n", 55 ^ 64);
return 1;
}
int hamming_distance(long int n, long int m)
{
int i = 0;
unsigned int count = 0;
for(i=0;i<64;i++)
{
if( (n&1) != (m&1) )
{
count++;
}
n >>= 1;
m >>= 1;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment