Coding Trainer
Number of 1 Bits
Problem
Number of 1 Bits
Given a positive integer n, write a function that returns the number of set bits (also known as the Hamming weight) in its binary representation.
Example 1:
Input: n = 11 (binary: 1011)
Output: 3
Example 2:
Input: n = 128 (binary: 10000000)
Output: 1
Example 3:
Input: n = 2147483645 (binary: 1111111111111111111111111111101)
Output: 30
Constraints:
- 1 ≤ n ≤ 2³¹ - 1