It's the same as count ones in a number


  • 0
    L

    class Solution {
    public:
    int hammingDistance(int x, int y) {
    int c = x ^ y;
    int cnt = 0;
    while(c > 0)
    {
    c &= (c - 1);
    ++cnt;
    }
    return cnt;
    }
    };


Log in to reply
 

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