I read the Wikipedia article on Hamming 's weight and noticed something interesting:
Thus, it is equivalent to Hamming distance from the entire zero line of the same length . For the most typical case, a string of bits is the number 1 in a string. In this binary case, it is also called popcount or side counting .
[emphasis mine]
So something happened to me. Can I calculate the Hamming distance between two lines on XOR ing , and then take the Hamming weight (POPCOUNT) of the resulting row?
Something along the lines of this (using gcc intrinsics):
#include <stdint.h> int hammingDistance (uint64_t x, uint64_t y) { uint64_t res = x ^ y; return __builtin_popcountll (res); }
Now, as to why I would like to do this, well, on some platforms, yes, it will just translate to gcc , calling the function that evaluates popcount . For example, on x64 without popcnt , gcc spits out ( Godbolt GCC Online ):
hammingDistance: sub rsp, 8 xor rdi, rsi call __popcountdi2 add rsp, 8 ret
OTOH, if you have a platform that supports POPCOUNT, for example, x64 models, including nehalem and after (which have popcnt ), you get ( Godbolt GCC Online ):
hammingDistance: xor rdi, rsi popcnt rax, rdi ret
which should be reset faster, especially once nested.
But back to the original question. Can you take the Hamming XOR weight of two lines to find their Hamming distance? i.e:
HD = HW (x xor y)
c gcc intrinsics hamming distance
haneefmubarak
source share