Number of 1 Bits
Count the 1 bits (set bits, Hamming weight) of a 32-bit number
DSA, Easy, Bit Manipulation
Which structure or algorithm, and what does it cost.
A control panel has 32 on/off switches. Its state is stored in a whole number n. The lowest bit of n is the first switch, and a switch is on when its bit is 1. Treat n as 32 bits with no sign. Return how many switches are on.
Examples
Constraints
nis a whole number from 0 to 4,294,967,295, so it fits in 32 bits.
Your answer must contain
- The simple method and its cost; a method that meets the target cost and why it is correct; code; time and memory cost; a hand trace of example 1.
Target Aim for O(1) memory and at most 32 rounds. The best method takes one round for each 1 bit.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Each switch is one binary digit of the number. Think about how to look at those digits one at a time.
Hint 2 the approach
Testing the lowest bit and shifting works, but it takes 32 rounds. Look for a trick that visits only the bits that are 1.
Hint 3 the key step
Write n and n minus 1 in binary, one above the other. Which bits changed? Find the one operation that keeps only the bits they share.
Walkthrough
Full walkthrough brute force to best
Brute force
Test each of the 32 positions in turn. Shift n right by the position, mask the lowest bit, and add it to a count. This is correct and always takes 32 rounds, even for n = 8, which has a single 1 bit. Time is a fixed 32 rounds and memory is O(1).
Better method
Test the lowest bit, add it to the count, shift n right by one, and stop when n is 0. The rounds end at the highest 1 bit, so a small number finishes early, but a number with its top bit set still takes 32 rounds.
Best method
Subtracting 1 from n turns its lowest 1 bit into 0 and turns every 0 below that bit into 1. Bits above stay as they were. So n and n minus 1 share no 1 bit at or below that position, and n AND (n minus 1) is n with exactly its lowest 1 bit cleared. Repeat until n is 0 and count the rounds. Each round removes one 1 bit, so the number of rounds is the number of 1 bits.
Hand trace
Run on 1000, which is 1111101000. Round 1 clears the lowest 1: 1111100000. Round 2: 1111000000. Round 3: 1110000000. Round 4: 1100000000. Round 5: 1000000000. Round 6: 0. That is six rounds, so the answer is 6.
Code
def count_set_bits(n):
count = 0
while n:
n &= n - 1 # clear the lowest 1 bit
count += 1
return countTime and memory
O(k) time, where k is the number of 1 bits, so at most 32 rounds, because each round clears one 1 bit. O(1) memory.
Common mistakes
- Shifting n right until it is 0 without testing the lowest bit. Shifting alone counts nothing.
- Using or instead of and with n minus 1. Only and clears the lowest 1 bit.
- Counting the digits of the binary text. For n = 8 that gives 4, but the answer is 1.
- Using a signed right shift in JavaScript or Java. For 4294967295 the value stays at -1 and the loop never ends. Use the unsigned shift.