Easy · Math

Count set bits in number

Given an integer n, return the number of 1 bits in its binary representation; a negative n is counted in its 64-bit two's-complement form (so -1 has 64).

Examples

Example 1

n = 13 (1101)

Output: 3 set bits

Example 2

n = 23 (10111)

Output: 4 set bits

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Math problems