Given a positive integer n
, write a function that returns the number of set bits in its binary representation (also known as the Hamming weight).
Example 1:
Input: n = 11
Output: 3
Explanation:
The input binary string 1011 has a total of three set bits.
Example 2:
Input: n = 128
Output: 1
Explanation:
The input binary string 10000000 has a total of one set bit.
Example 3:
Input: n = 2147483645
Output: 30
Explanation:
The input binary string 1111111111111111111111111111101 has a total of thirty set bits.
Constraints:
1 <= n <= 231 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine the number is a sequence of light switches, either on (1) or off (0). The brute force strategy is to simply check each switch individually to see if it's on.
Here's how the algorithm would work step-by-step:
def number_of_one_bits_brute_force(number):
number_of_ones = 0
# Iterate through the bits of the number
for i in range(32):
# Check the least significant bit
if (number & 1):
# If the bit is 1, increment the counter
number_of_ones += 1
# Right shift the number to check the next bit
# This simulates moving to the next switch
number = number >> 1
return number_of_ones
The most efficient way to count the number of 1s in a binary number takes advantage of a bitwise trick. This trick cleverly isolates and removes the rightmost 1 in each step, allowing us to count how many steps it takes to reach zero, which equals the number of 1s.
Here's how the algorithm would work step-by-step:
def count_number_of_set_bits(number):
number_of_bits_set = 0
# Iterate until the number becomes zero
while number:
# This operation isolates the rightmost 1
number &= (number - 1)
number_of_bits_set += 1
return number_of_bits_set
Case | How to Handle |
---|---|
Input is zero (0) | The algorithm should correctly return 0 since the binary representation of 0 has no set bits. |
Input is a small positive integer (e.g., 1, 2, 3) | Verify the algorithm accurately counts set bits for fundamental cases like 1 (0001), 2 (0010), 3 (0011). |
Input is a large positive integer (close to the maximum unsigned integer value) | Test with a number near the maximum unsigned integer limit to check for integer overflow during bitwise operations. |
Input is a power of 2 (e.g., 2, 4, 8, 16) | A power of 2 has only one set bit, so the algorithm must correctly return 1 in these scenarios. |
Input with alternating 0s and 1s in its binary representation (e.g., 5, 10) | These inputs require the algorithm to correctly iterate through and count all set bits without skipping any. |
Input that is the maximum unsigned integer value | This tests the loop's termination condition and ensures no errors occur at the boundaries of the unsigned integer range. |
Language-specific: Potential integer overflow during calculations (if applicable) | Ensure chosen method doesn't exceed the maximum representable value to avoid incorrect results. |
Input with a high density of set bits | This tests performance and accuracy when most bits are set to one. |