C++_Bit Manipulation _AC


  • 0
    class Solution {
    public:
    int hammingDistance(int x, int y) {
        if(x == y) return 0;
        int cnt = 0;
        while(x || y){
            if((x&1) != (y&1)) cnt++;
            x = x>>1;
            y = y>>1;
        }
        return cnt;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.