Math Fundamentals18 sections · 814 units
Open in Course

Problem - Number of 1 Bits

LeetCode 191

Here's your first problem: count how many bits are set to 1 in the binary representation of a positive integer nn.

For example, if n=13=1101n = 13 = 1101, the answer is 3 (three 1s). If n=7=111n = 7 = 111, the answer is 3.

Before reading the solution, think: how would you check each bit? How many bits do you need to check?