Problem
You get an unsigned 32-bit integer n as a normal integer value. An unsigned 32-bit integer is a whole number from 0 to 2^32 - 1.
The binary form of a number uses only the digits 0 and 1. Each digit is a bit. Return the number of bits with the value 1 in the binary form of n.
Example 1
- Input:
- n = 13
- Output:
- 3
- Explanation:
- In binary, 13 is
1101, which has three bits with the value 1.
Example 2
- Input:
- n = 256
- Output:
- 1
- Explanation:
- In binary, 256 is
100000000. Only one bit is 1.
Example 3
- Input:
- n = 4294967294
- Output:
- 31
- Explanation:
- This value is
2^32 - 2. In binary, it has 31 bits with the value 1 and then one 0.
Constraints
- 0 <= n <= 2^32 - 1
Follow-up
Suppose that a program calls the function many times. How can you make each call faster?
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Google
Leetcode 191. Number of 1 Bits
Reported Oct 16, 2024 · 1 source report
JUNIOR
Source snapshot: Nov 12, 2026
View source