Count Set Bits
Given a non-negative integer n, return the number of 1 digits in its binary representation. A binary digit equal to 1 is called a set bit. The value 0 has no…
Given a non-negative integer `n`, return the number of `1` digits in its binary representation. A binary digit equal to `1` is called a set bit. The value `0` has no set bits.
- `0 <= n <= 10^9` - Return the count as an integer.
Example 1: input n = 13; output 3. Thirteen is 1101 in binary, which contains three set bits.
Example 2: input n = 16; output 1. Sixteen is 10000 in binary, which contains one set bit.
Example 3: input n = 0; output 0. Zero has no set bits.
Category: Math. Topic: math. Difficulty: Easy. Patterns: math, bit-manipulation.