Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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