Problem overview
The method HammingWeight receives a non-negative int n and returns how many 1 bits its binary form has, also called its Hamming weight. For n = 11, the binary form is 1011, so the result is 3. For 128, the binary form is 10000000: one 1 bit. For 2147483645, 30 of its 31 binary digits are 1, so the result is 30.
Approach: remove one 1 bit per pass
The obvious way is to test all 32 bits, shifting right each time. That always takes 32 steps. This lesson's loop runs only once per 1 bit, so 128 takes one pass, not 32.
The idea rests on what subtracting 1 does in binary. The lowest 1 bit of n turns into 0, every 0 below it turns into 1, and the higher bits stay the same. For 12 (1100), 12 - 1 = 11 (1011). Now the two numbers have a 1 in the same column only above the old lowest 1 bit, so n & (n - 1) is n with exactly its lowest 1 bit cleared: 1100 & 1011 = 1000.
Repeat that until n is 0 and count the repetitions. Each repetition removes one 1 bit, so the count equals the number of 1 bits.
Approach compared with checking every bit
The straightforward alternative tests each of the 32 positions with (n >> i) & 1 and adds the results. It gives the same answers (3, 1 and 30 for 11, 128 and 2147483645), but it always makes 32 passes, whatever n is. Clearing the lowest bit makes the work depend on the number of 1 bits instead, which is why the lesson asks for O(k).
.NET also has a built-in, System.Numerics.BitOperations.PopCount, which takes a uint and returns the same count (3 for 11). The exercise asks you to build the counting yourself.
Step by step
- Create
count, anintstarting at 0. - Loop while
n != 0. After the last 1 bit is removed,nis 0 and the loop ends. - Clear the lowest 1 bit:
n = n & (n - 1);. The&operator is bitwise AND: a result bit is 1 only where both numbers have a 1. - Add 1 to
countin the same pass. - Return
count.
Tracing n = 11, the value of n in binary after each pass is:
1010
1000
0
Three passes, so the answer is 3.
The solution in C#
class Solution {
public int HammingWeight(int n) {
int count = 0;
while (n != 0) {
n = n & (n - 1);
count++;
}
return count;
}
}
Details worth knowing:
- Parameters are copies. Changing
ninside the method doesn't change the caller's variable. - Precedence. In C#,
-binds tighter than&, son & n - 1would mean the same, but the parentheses make the intent clear. - No overflow here.
n - 1only runs whilenis not 0, andnis never negative, so it can't go below 0 or wrap.
Complexity
Time: O(k), where k is the number of 1 bits, at most 31 for these values. Space: O(1): two int variables.
Edge cases and common mistakes
- Negative values. They are outside the lesson's range. The loop still ends for them:
-1, with all 32 bits set, returns 32. - Zero. The loop never runs and the method returns 0.
- The largest value.
int.MaxValue(2147483647) has 31 ones and returns 31. - Powers of two. Each has one 1 bit, so
1and128both return 1. - Forgetting the increment.
countmust go up once per pass, one for each bit removed.
Testing
11gives3.128gives1.2147483645gives30.0gives0.2147483647gives31.
