C
源码
#include "stdio.h"
int hamming_distance(unsigned x, unsigned y)
{
int dist = 0;
unsigned val = x ^ y;
// Count the number of bits set
while (val != 0)
{
// A bit is set, so increment the count and clear the bit
dist++;
val &= val - 1;
}
// Return the number of differing bits
return dist;
}
int main(void)
{
printf("%d \r\n", hamming_distance(0b0001, 0b0100));
printf("%d \r\n", hamming_distance(0b1100, 0b0011));
printf("%d \r\n", hamming_distance(0b0011, 0b0011));
return 0;
}
makefile
源码
object := test.o
test:$(object)
gcc -o test $(object)
test.o : test.c
gcc -c test.c
.PHONY:clean
clean:
-rm test *.o