Ctrl K

Count the Set Bits

Easy
You are given a non-negative integer value. Count how many ones its binary representation contains. If the function is called a huge number of times, how would you speed it up?
Example 1
value=13 → 3

13 is 1101, which has three ones.

Example 2
value=64 → 1

64 is 1000000, which has a single one.

Example 3
value=0 → 0
Constraints
  • 0 ≤ value ≤ 2³¹ - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFlip the Bit Order EasyBoth Cases Stretch EasyK-th Largest Corner XOR MediumSum of Binary Strings EasyBalanced Tree from a Sorted List Easy
loading Python…
Input
value=13
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 131 tests on the server, hidden ones included.