leetcode:Number of 1 Bits

1:

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n)
        {
            if(n&0x1==1)
            {
                count++;
            }
            n=n>>1;
        }
        return count;
    }
};
:2:

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n)
        {
            count++;
            n=(n-1)&n;
        }
        return count;
    }
};


你可能感兴趣的:(leetcode:Number of 1 Bits)