EasyBit Manipulation

Number of 1 Bits

You are given an unsigned integer n that fits in 32 bits. Return the number of 1 bits in its binary representation (its Hamming weight).

n = 23 (10111) → 4n = 2147483645 → 30

Try:
CALL
1 / 1

Code

The highlighted line runs next. The pill shows the live values in the running call.

Variables

The data structures the code is building

32-bit registers

Bit 31 on the left, bit 0 on the right

Time and space complexity

n is at most 32 bits wide, so every solution is O(1). The highlighted row is the solution being traced.

Keyboard: ← → step, Space play or pause. n from 0 to 2³¹ − 1.