My C++ Solution using bit manipulation


  • 0
    B
    
    class Solution {
    public:
         int hammingDistance(register int x, int y) {
            register int count = 0;
            x ^= y;
            while(x!=0){
                ++count;
                x &= (x - 1);
            }
            return count;
        }
    };

Log in to reply
 

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