Java Solution (as few iterations as possible)


  • 0
    X

    We don't have to check every bit of the Integers.

        public int hammingDistance(int x, int y) {
            int number = 0;
    	int mask = 1;
    	while (x != 0 || y != 0) {
                number += (mask & x) ^ (mask & y);
                x >>= 1;
                y >>= 1;
            }
    	return number;
        }
    

Log in to reply
 

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