Problem overview
The method hammingWeight receives a non-negative integer n, up to 2,147,483,647, and returns how many 1 bits its binary form has.
For n = 11 the binary form is 1011, which has three 1 bits, so the result is 3. For 128, which is 10000000, the result is 1. For 2147483645, which is thirty-one digits with a single 0, the result is 30.
The note 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 loop tests the lowest bit of n, shifts n right and repeats, which takes one pass for every binary digit, 1 or 0. This lesson uses a trick that skips the zeros: each pass removes exactly one 1 bit, so the loop runs k times.
The trick rests on what subtracting 1 does in binary. The lowest 1 bit of n becomes 0, every 0 below it becomes 1, and the higher bits stay the same. For n = 12, which is 1100, n - 1 is 11, which is 1011. Taking the bitwise AND of the two keeps only the bits that are 1 in both, and the changed bits can't match, so n & (n - 1) is n with its lowest 1 bit removed: 1100 & 1011 is 1000. Repeat until n is 0 and count the passes.
For n = 11 the passes are:
n n - 1 n & (n - 1)
1011 1010 1010
1010 1001 1000
1000 0111 0000
Three passes, so three 1 bits.
Step by step
- Start
countat 0, declared withletbecause it changes. - Loop while
n !== 0. Each pass removes one 1 bit, sonreaches 0 right after its last 1 bit is gone. A number with no 1 bits, such as 0, skips the loop. - Clear the lowest 1 bit:
n = n & (n - 1). The bitwise AND gives a 1 only where both numbers have a 1. - Add 1 to
countwith++, since one bit was removed. - Return
count.
The solution in TypeScript
class Solution {
hammingWeight(n: number): number {
let count = 0;
while (n !== 0) {
n = n & (n - 1);
count++;
}
return count;
}
}
Some TypeScript details:
- Parameters can be reassigned.
nis an ordinary local variable inside the method, so the loop can overwrite it. The caller's number is not affected, because numbers are passed by value. - Bitwise operators work on 32-bit integers.
numberis a double, but&converts both operands to signed 32-bit integers first and gives back anumber. For the values in this lesson, 0 up to 2147483647, that conversion changes nothing. - The parentheses matter. Subtraction binds tighter than
&, son & n - 1would also meann & (n - 1), but the parentheses make the intent clear. - Types are erased. The annotations on
nand the return value exist only for the compiler.
Complexity
Time: O(k), where k is the number of 1 bits. Each pass removes exactly one of them, and the loop stops when none are left. For these inputs that is at most 31 passes.
Space: O(1). The method keeps one counter and the number it is changing.
Edge cases and common mistakes
- Zero.
hammingWeight(0)returns0: the loop body never runs. - A power of two.
128has one 1 bit, so it takes a single pass and the result is1. - All ones.
2147483647, thirty-one 1 bits, returns31. - Looping on the wrong condition. Testing
n > 0works for the lesson's inputs, butn !== 0also handles values that bitwise operators turn negative. - Forgetting to update
n. Writingn & (n - 1)without assigning the result leavesnunchanged, and the loop never ends. - Beyond the lesson's range. A number such as
4294967295is outside the constraints, but bitwise operators treat it as the 32-bit pattern of all ones, and this code returns32for it. Numbers of 4,294,967,296 or more lose their upper bits, so this approach isn't meant for them.
Testing
n = 11gives3.n = 128gives1.n = 2147483645gives30.n = 0gives0.n = 1gives1.n = 255gives8.n = 2147483647gives31.
