My C++ Solution


  • 0
    N
    class Solution {
    public:
        int hammingDistance(int x, int y) {
            int xBit, yBit;
            int diff = 0;
            for(int i = 0; i < 32; i++){
                xBit = x % 2;
                yBit = y % 2;
                
                x = x >> 1;
                y = y >> 1;
                
                if (xBit != yBit)
                    diff++;
            
            }
            
            return diff;
        }
    };
    

Log in to reply
 

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