Number of 1 Bits

Given a non-negative integer n, return the number of set bits in its binary representation. Pattern focus: Clear Lowest Bit. Repeatedly remove the lowest set bit using n & (n - 1) until the number becomes zero.

Input Format

n = non-negative integer

Output Format

count of set bits in n

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • n must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

n = 11

Output:

3

Explanation:

11 in binary is 1011, which has three set bits.

Example 2:

Input:

n = 0

Output:

0

Explanation:

Zero has no set bits.

Loading...
Number of 1 Bits - Bit Manipulation DSA Problem