sundeepblue
4/21/2014 - 2:22 PM

compute the Hamming distance of two integers (considered as binary values, that is, as sequences of bits)

compute the Hamming distance of two integers (considered as binary values, that is, as sequences of bits)

int hamming_distance(unsigned int a, unsigned int b) {
    int val = a ^ b;
    int dist = 0;
    while(val != 0) {
        val = val & (val-1);
        dist++;
    }
    return dist;
}