Hamming Distance

来源:互联网 发布:linux wget https 404 编辑:程序博客网 时间:2024/04/30 19:40

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:

0 ≤ xy < 231.

点击打开链接

int hammingDistance(int x, int y) {    int res = 0;    for(int i = 0; i < 31; i++){        if((x&(1<<i))^(y&(1<<i)))res++;    }    return res;}


0 0
原创粉丝点击