v1 = [1 1 1 0]
v2 = [0 1 0 0]
HD = 2
But is the hamming distance not just a basic XOR operation:
INPUT OUTPUT
A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
Bye, Jojo
Where does 6 come from?
v1 =
1 1 1 0
v2 =
0 1 0 0
x_or =
1 0 1 0
sum_xor =
2
Bye, Jojo
-- snip.c --
#include <stdio.h>
#include <stdint.h>
/* unsigned char bit_tbl[256] = { ... } */
uint32_t hd(uint32_t a, uint32_t b);
int main(void) {
uint32_t a = 6, b = 42;
printf("hd(%" PRIu32 ", %" PRIu32 ") = %" PRIu32 "\n",
a, b, hd(a, b));
return 0;
}
uint32_t hd(uint32_t a, uint32_t b) {
uint32_t c = a ^ b;
return bit_tbl[c & 0xffu] +
bit_tbl[(c >> 8) & 0xffu] +
bit_tbl[(c >> 16) & 0xffu] +
bit_tbl[(c >> 24) & 0xffu];
}
-- snip.c --
Also see this page:
<http://infolab.stanford.edu/~manku/bitcount/bitcount.html>
Hamming distance is a bitwise XOR, followed by counting the 1 bits
in that result.