Problem overview
The function hammingWeight receives a non-negative integer n and returns how many 1 bits its binary form has. This count is also called the Hamming weight. For n = 11, the binary form is 1011, so the answer is 3. For n = 128, which is 10000000, the answer is 1. For n = 2147483645, which is thirty-one binary digits with a single 0, the answer is 30.
The lesson asks for O(k) time, where k is the number of 1 bits, and O(1) extra space.
Approach: remove one 1 bit per pass
The obvious way is to look at all the bits: test the last bit with n & 1, shift right, and repeat until n is 0. That does one pass per binary digit, including the 0s. The lesson uses a faster trick that does one pass per 1 bit.
Look at what subtracting 1 does in binary. The lowest 1 bit becomes 0, every 0 below it becomes 1, and the bits above stay the same. For example, 12 is 1100 and 11 is 1011. Now AND the two numbers: the changed low part cancels out (a 1 meets a 0 everywhere), and the high part is kept. So n & (n - 1) is n with its lowest 1 bit removed: 1100 & 1011 is 1000.
Repeat that until n is 0 and count the repetitions. Each repetition removes exactly one 1 bit, so the count is the number of 1 bits.
Here is n = 11 printed after each pass of the loop, in binary:
1011
1010
1000
The first line is n before the loop starts. After three passes n is 0, and three is the answer.
Step by step
- Create
countwith:=and start it at 0. - Write the loop. Go has no
while, but aforwith only a condition does the same job:for n != 0. - Remove the lowest 1 bit with
n = n & (n - 1). - Add 1 to
countwithcount++. One pass removed one bit. - Return
countafter the loop.
The solution in Go
func hammingWeight(n int) int {
count := 0
for n != 0 {
n = n & (n - 1)
count++
}
return count
}
A few Go details:
- Parameters are copies. Assigning to
ninside the function changes only the function's own copy, so the caller's number is untouched. - Operator precedence. In Go,
&binds tighter than+and-, so the parentheses aroundn - 1are required: they make the subtraction happen first. intis 64 bits here. The lesson's values fit easily, with room to spare.
Complexity
Time: O(k), where k is the number of 1 bits. Each pass removes one 1 bit, so the loop runs exactly k times: 3 times for 11, once for 128, and 30 times for 2147483645. For values up to 2147483647 that is at most 31 passes.
Space: O(1). The function keeps two integers, count and n.
Edge cases and common mistakes
- Zero.
hammingWeight(0)skips the loop and returns 0. - A power of two. There is a single 1 bit, so the loop runs once:
hammingWeight(128)is 1. - Counting before the loop condition. Increment
countinside the loop, once per removed bit. Starting it at 1, or incrementing after the loop, is off by one. - Dropping the parentheses.
n & n - 1is read as(n & n) - 1, because&binds tighter than-in Go. Forn = 12it gives 11 instead of 8, so the loop would only count down by one and returnn. - Using
n % 2andn / 2. This also works, but it passes over the 0 bits too, which is the slowerO(log n)approach.
Testing
n = 11gives3.n = 128gives1.n = 2147483645gives30.n = 0gives0.n = 1gives1.n = 7gives3.
