[LeetCode]461. Hamming Distance

来源:互联网 发布:优化驱动器有什么好处 编辑:程序博客网 时间:2024/05/16 10:03

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 ≤ x, y < 2^31.

My solution

class Solution{public:    int hammingDistance(int x, int y){        int diff = x ^ y;        int count = 0;        //bitCount        while(diff != 0){            count +=diff % 2;            diff /= 2;        }        return count;    }}
0 0
原创粉丝点击