Number of 1 Bits
Write a function that takes an integer and returns the number of set bits (1s) in its binary representation, also known as the Hamming weight.
Open official problem prompt ↗Count exactly how many 1 bits appear in the binary form of the given integer.
Popping filled bubbles one at a time: each pop clears the lowest remaining filled bubble, and you count pops until the sheet is empty.
- Input
- n = 11 (binary 1011)
- Output
- 3
- Why
- 1011 contains three 1 bits.
The input is treated as an unsigned integer (up to 32 bits)1 <= n <= 2^31 - 1 in the modern signature