Leetcode 191 Solution

This article provides solution to leetcode question 191 (number-of-1-bits)

https://leetcode.com/problems/number-of-1-bits

Solution

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int cnt = 0;

        while (n)
        {
            n = n & (n - 1);
            cnt++;
        }

        return cnt;
    }
};