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 <= 2^31 - 1
Follow up: If this function is called many times, how would you optimize it?
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:
We want to figure out how many '1's are in a given number when we write it in binary (using only 1s and 0s). The brute force method is simple: we examine each digit of the binary representation one by one.
Here's how the algorithm would work step-by-step:
def count_number_of_one_bits_brute_force(number):
number_of_one_bits = 0
# Iterate until the number becomes zero
while number:
# Check the rightmost bit if it is one
if number & 1:
number_of_one_bits += 1
# Right-shift the number by 1. This moves to the next bit.
number = number >> 1
return number_of_one_bits
The most efficient way to count the number of ones in a binary number takes advantage of a bitwise trick. This trick cleverly isolates and removes the rightmost one bit in each step until the number becomes zero, and each removal represents a 'one' we've counted.
Here's how the algorithm would work step-by-step:
def count_number_of_set_bits(number):
set_bit_count = 0
# Iterate until the number becomes zero
while number:
# This operation isolates the rightmost 1-bit
number &= (number - 1)
set_bit_count += 1
return set_bit_count
Case | How to Handle |
---|---|
Input is 0 | The algorithm should correctly return 0, as the binary representation has no 1s. |
Input is the maximum unsigned integer | The algorithm should correctly return the number of bits in the integer type (e.g., 32 for a 32-bit integer). |
Input is a power of 2 (e.g., 1, 2, 4, 8) | The algorithm should return 1, as only one bit is set. |
Input has alternating 0s and 1s | The bitwise operations will correctly count the 1s regardless of their pattern. |
Language-specific integer overflow | Since the input is an unsigned integer, overflow isn't a primary concern within the given context of bit counting; the algorithm will operate on the bits present within the data type's capacity. |
Negative Input | Since the input is an unsigned integer, negative numbers are not a valid input; the function should assume valid input within the unsigned integer range. |
Very large sparse inputs (mostly 0s with a few scattered 1s) | Algorithms like the bit manipulation trick `n &= (n - 1)` are designed to efficiently handle this case by iterating only as many times as there are 1s. |
Integer that is one less than a power of 2 (e.g., 3, 7, 15) | The algorithm should correctly calculate the number of 1s; all bits up to the leading bit will be set to 1. |