C++ solution using bit manipulation ^ and >>


  • 0
    B
    class Solution {
    public:
        int hammingDistance(int x, int y) 
        {
            int Temp = x ^ y;
    	int Sum = 0;
            for (unsigned int i=0; i<32; i++)
    	    if ((Temp >> (31 - i)) & 1 == 1) 
                    Sum++;
       
        	return Sum;
        }
    };
    

Log in to reply
 

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