Loading
Leetcode / 191. Number of 1 Bits

Pick a programming language:

Here is the source code for the solution to this problem.

class Solution {
    public int hammingWeight(int n) {
        int count = 0;
        while (n != 0) {
            int digit = n & 1;
            if (digit == 1) {
                count++;
            }
            n = n >> 1;
        }
        return count;
    }
}
Did you like the lesson? 😆👍
Consider a donation to support our work: