Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hamming distance = XOR?

1,001 views
Skip to first unread message

saneman

unread,
May 28, 2008, 4:58:05 AM5/28/08
to
The hamming distance (HD) measures the number of different bits in two
bitpatterns:

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


Joachim Schmitz

unread,
May 28, 2008, 5:01:51 AM5/28/08
to

Joachim Schmitz

unread,
May 28, 2008, 5:05:34 AM5/28/08
to
Actually no, but it's the number of bits set after the XOR, not the value of
the XOR operation (which in your example would be 6 in decimal)

Bye, Jojo


saneman

unread,
May 28, 2008, 5:19:40 AM5/28/08
to

"Joachim Schmitz" <nospa...@schmitz-digital.de> skrev i en meddelelse
news:g1j79o$kdr$1...@online.de...

Where does 6 come from?

v1 =

1 1 1 0


v2 =

0 1 0 0


x_or =

1 0 1 0


sum_xor =

2

Joachim Schmitz

unread,
May 28, 2008, 6:06:02 AM5/28/08
to
01010 is 18 decimal. 2nd and 4th bit set. In the original it was 0110, 2nd
and 3rd bit set, and that is 6 decimal.

Bye, Jojo


vipp...@gmail.com

unread,
May 28, 2008, 12:33:12 PM5/28/08
to
No, it's a XOR operation and then take the result and apply it to a
function that counts bits in an integer.

-- 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>

Gordon Burditt

unread,
May 28, 2008, 4:17:26 PM5/28/08
to

Hamming distance is a bitwise XOR, followed by counting the 1 bits
in that result.

0 new messages