Number of 1 Bits
Given a 32-bit unsigned integer n, return the number of 1 bits in its binary representation.
This is also known as the Hamming weight of the integer.
For example, the binary representation of 11 is:
00000000000000000000000000001011
It contains three 1 bits, so the answer is 3.
Example 1
Input
n = 11
Output
3
Explanation
The 32-bit representation of 11 is:
00000000000000000000000000001011
There are three 1 bits.
Example 2
Input
n = 128
Output
1
Explanation
The 32-bit representation is:
00000000000000000000000010000000
There is only one 1 bit.
Constraints
Hints:
Hint 1
Check the rightmost bit using:
n & 1
Hint 2
After checking a bit, shift the number right:
n >>= 1
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 23, 2026
Last updated
Aug 23, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output