You are given an unsigned integer n. Return the number of 1 bits in its binary representation.
You may assume n is a non-negative integer which fits within 32-bits.
Example 1:
Input: n = 00000000000000000000000000010111
Output: 4Example 2:
Input: n = 01111111111111111111111111111101
Output: 30
You should aim for a solution with O(1) time and O(1) space.
The given integer is a 32-bit integer. Can you think of using bitwise operators to iterate through its bits? Maybe you should consider iterating 32 times.
We iterate 32 times (0 to 31) using index i. The expression (1 << i) creates a bitmask with a set bit at the i-th position. How can you check whether the i-th bit is set in the given number? Maybe you should consider using the bitwise-AND ("&").
Since the mask has a set bit at the i-th position and all 0s elsewhere, we can perform a bitwise-AND with n. If n has a set bit at the i-th position, the result is positive; otherwise, it is 0. We increment the global count if the result is positive and return it after the iteration.
We are asked to count how many 1 bits are present in the binary representation of an integer n.
This value is also known as the Hamming Weight or population count.
A straightforward way to do this is to:
1) or not (0)Since integers are typically represented using 32 bits, we can safely check all 32 bit positions.
At each position:
1 at that position using 1 << i&) to test whether that bit is set in nres = 0.i from 0 to 31:i-th bit set:mask = 1 << in:(mask & n) != 0, increment resres.We want to count the number of 1 bits in the binary representation of an integer n.
Instead of checking every bit position explicitly, we can:
nAt each step:
n & 1 tells us whether the current least significant bit is 1n right by one (n >>= 1) moves us to the next bitWe repeat this until n becomes 0.
res = 0.n > 0:1 (n & 1):resn one bit to the right (n >>= 1)n becomes 0, all bits have been processed.res.We want to count the number of 1 bits in the binary representation of an integer n (Hamming Weight).
A very efficient trick comes from this key observation:
1 from a number flips the rightmost 1 bit to 0 and turns all bits to its right into 1n & (n - 1) removes the rightmost 1 bit from nSo every time we do:n = n & (n - 1) we eliminate exactly one 1 bit.
This means:
1 bits0That’s why this approach is considered optimal.
res = 0.n is not zero:n = n & (n - 1) to remove the rightmost 1 bitres by 1n becomes 0, all 1 bits have been removed.res.We want to find the Hamming Weight of a number, which means counting how many 1 bits are present in its binary representation.
Most programming languages provide:
Instead of manually checking each bit using bit manipulation, we can rely on these built-in features. This makes the solution short, easy to understand, and less error-prone, especially for beginners.
This approach focuses on clarity and simplicity, not on low-level optimizations.
n into its binary representation1 bits in that representation.In some languages, right-shifting a signed negative integer fills with 1s (arithmetic shift) instead of 0s (logical shift). This can cause infinite loops when using while (n != 0) with negative inputs. Use unsigned types or the >>> operator in languages that support it.
While n / 2 and n >> 1 produce the same result for positive integers, division can behave differently for negative numbers and is generally slower. Stick to bitwise operations for clarity and consistency with the problem's intent.