Number of 1 Bits — Bit Manipulation

Count how many bits are switched on in the binary form of an unsigned machine word.

Bit Manipulation · Easy

The problem

A hardware register holds a 32-bit value, and you want to know how many of its switches are on. Given a non-negative whole number that fits in 32 bits, count how many of its binary digits are 1.

The story

A hardware register holds a 32-bit value from 0 to 4294967295, and each binary digit drives one switch. Count how many switches are on, that is, how many 1s appear in the number's binary form.

What to return

Constraints

Example

Input
n = 13
Output
3

Solve it

More in Bit Manipulation

All algorithm problems