Powers and Popcount
What is this?
Picture a row of light switches again. "Popcount" simply means counting how many switches are flipped on. And a "power of two" is a number whose row has exactly one switch on โ like a single lit bulb in a dark hallway. These problems are about spotting that lone-light pattern and counting lit switches quickly.
๐ก Fun fact: Counting set bits is so common that modern CPUs have a dedicated single instruction for it called POPCNT, and the operation traces back to the "side-counter" circuits in early IBM mainframes.
๐ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.
Core idea: a number is a power of two exactly when its binary form has a single set bit, and the trick
n & (n-1)clears the lowest set bit. Counting how many times you can clear before reaching zero gives the popcount, and checking how many set bits remain tells you which power you're looking at.
The pattern
Two reflexes unlock this whole bucket. First, n & (n-1) flips off the rightmost 1: subtracting one turns the lowest set bit into zeros below it, and the AND keeps only the higher bits. Loop it and count iterations to get the number of set bits, touching the value once per set bit rather than once per bit.
Second, a power of two has exactly one set bit, so n > 0 && (n & (n-1)) == 0 settles "is this a power of two?" in one expression. Powers of four are a special case: they're powers of two whose single bit sits at an even position, which you verify with a fixed mask of the even bit positions. Powers of three don't have a clean binary signature โ three is odd โ so you either divide by three until you can't, or test divisibility against the largest power of three that fits the integer width.
The problems
- Number of 1 Bits โ count set bits; loop
n &= n-1and tally, which runs in as many steps as there are ones. - Power of Two โ a single set bit; return
n > 0 && (n & (n-1)) == 0. - Power of Three โ no tidy bit pattern; divide by three repeatedly or check divisibility by the max power of three.
- Power of Four โ a single set bit at an even position; combine the power-of-two test with an even-position mask.
Key takeaways
n & (n-1)clears the lowest set bit โ the workhorse for both counting bits and the power-of-two test.- Popcount via that trick costs one step per set bit, not one per bit width.
- Power of two means exactly one set bit; power of four means that bit lands on an even position.
- Power of three has no bit signature โ fall back to division or a divisibility check.
- Guard against
n <= 0before any single-bit test, since zero and negatives slip past a naive mask.
Core idea: Counting the 1s in an integer is the Hamming weight, and Brian Kernighan's
n &= n - 1clears one set bit per step โ so you loop once per 1, not once per bit.
Problem, rephrased
You run a notification service. Every user carries a 32-bit subscription bitmask where each bit means "subscribed to topic k." You need to know, for any user, how many topics they're subscribed to โ that is, how many bits in their mask are set to 1.
Given an integer, return the count of 1 bits in its binary representation.
| Input (decimal) | Binary (8-bit view) | Set bits | Output |
|---|---|---|---|
0 |
0000 0000 |
none | 0 |
1 |
0000 0001 |
one | 1 |
11 |
0000 1011 |
three | 3 |
255 |
1111 1111 |
eight | 8 |
That's it. No tricks in the statement โ the interesting part is how fast you can do it.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson