[leetcode_191] Number of 1 Bits

Count the number of 1s in the binary representation of an integer. A straightforward simulation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n > 0)
        {
            count += n%2;
            n /= 2;
        }
        return count;
    }
};