Solution in Java


  • 0
    W
    class Solution {
        public int hammingDistance(int x, int y) {
            int c = 0;
    		int xy = (x^y);
    		while(xy>0) {
    			if((xy&1)==1) {c += 1;}
    			xy >>= 1;
    		}
    		return c;
        }
    }
    

    Because in Binary, n = (n-1) + 1


Log in to reply
 

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