Number of 1 Bits
easyCount the number of set bits (1s) in a binary number (Hamming Weight)
Number of 1 Bits
Key Insight
n & (n-1) clears the lowest 1-bit. Count iterations until n=0.
Step 1Input
n
11
=0
0
0
0
1
0
1
1
76543210
1011 has ? ones
Count 1-bits in 11 (binary: 1011)
1 / 5