C simple solution & easy to understand


  • 0
    M
    int hammingDistance(int x, int y) {
        
        /* Find the different bits */
        int value = x^y;
        int count = 0;
        
        /* For example value = (1111)
        (1111)&(1110)=(1110)  =>1
        (1110)&(1101)=(1100) =>2
        (1100)&(1010)=(1000) =>3
        (1000)&(0001)=(0000) =>4  */
    
        while(value)
        {
            count++ ;
            value &= (value - 1);
        }
        return count;
    }
    

Log in to reply
 

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