Quick Overview

A bit-manipulation task that asks how many bits are set to 1 in a 32-bit unsigned integer. It tests understanding of binary representation and bitwise operations, and correct handling of inputs above the signed 32-bit range.

Count the Set Bits in a 32-Bit Unsigned Integer

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a non-negative integer that fits in 32 unsigned bits, return how many of its bits are 1. ### Function Signature ```python def count_set_bits(n: int) -> int: ``` ### Rules - `n` is read as an unsigned 32-bit value; return the number of 1 bits among its 32 bits. - The result is between 0 and 32 inclusive. ### Constraints - `0 <= n <= 4294967295`, that is, `n <= 2^32 - 1`. - Inputs above `2^31 - 1` (2147483647) are valid: in a language whose default integer is a signed 32-bit type, the input must be handled as unsigned. ### Examples **Example 1** ```text Input: n = 11 Output: 3 ``` 11 is `1011` in binary. **Example 2** ```text Input: n = 2147483648 Output: 1 ``` Only the highest of the 32 bits is set. **Example 3** ```text Input: n = 4294967295 Output: 32 ``` All 32 bits are set.

Overview: A bit-manipulation task that asks how many bits are set to 1 in a 32-bit unsigned integer. It tests understanding of binary representation and bitwise operations, and correct handling of inputs above the signed 32-bit range.

Given a non-negative integer `n` that fits in 32 unsigned bits, return how many of its bits are 1. Read `n` as an unsigned 32-bit value and count the 1 bits among its 32 bits (bit 0 through bit 31). The result is always an integer between 0 and 32 inclusive. Inputs above `2^31 - 1` (2147483647) are valid, up to `2^32 - 1` (4294967295). Because `n` can exceed `2^31 - 1`, it is passed as a 64-bit integer in Java (`long`) and C++ (`long long`), and as an ordinary number in JavaScript. In every language it must be handled as an unsigned 32-bit value, so the highest bit is never a sign bit. The return value never exceeds 32 and fits in a 32-bit `int`. Implement `count_set_bits(n)`, which returns the count as an integer. **Example 1** ```text Input: n = 11 Output: 3 ``` 11 is `1011` in binary, which has three 1 bits. **Example 2** ```text Input: n = 2147483648 Output: 1 ``` 2147483648 is `2^31`: only the highest of the 32 bits is set. **Constraints** - `0 <= n <= 4294967295`, that is, `n <= 2^32 - 1`. - Inputs above `2^31 - 1` (2147483647) are valid and must be handled as unsigned. - The result is between 0 and 32 inclusive.

Constraints

  • 0 <= n <= 4294967295, that is, n <= 2^32 - 1.
  • Inputs above 2^31 - 1 (2147483647) are valid and must be handled as unsigned 32-bit values; because n can exceed 2^31 - 1, Java takes it as long and C++ as long long.
  • The result is between 0 and 32 inclusive.

Examples

Input: (0,)

Expected Output: 0

Explanation: Minimum valid input: no bits are set.

Input: (1,)

Expected Output: 1

Explanation: Single set bit at the lowest position, bit 0.

Hints

  1. The answer depends only on the 32 binary digits of n, so think about how to inspect or remove them one at a time.
  2. Values above 2147483647 are valid: make sure nothing in your approach treats bit 31 as a sign bit.
  3. Check the extremes: n = 0 has no 1 bits, and n = 4294967295 has all 32 bits set.

Loading coding console...

Show the approach

Approach

Repeatedly clear the lowest set bit. For any positive x, x & (x - 1) equals x with its lowest 1 bit turned off: subtracting 1 flips that bit to 0 and every lower 0 bit to 1, and the AND discards those flipped low bits. Invariant: count + (number of 1 bits in x) always equals the number of 1 bits in n, starting from count = 0 and x = n masked to 32 bits. Each iteration lowers the 1-bit count of x by exactly one and raises count by one, so when x reaches 0, count is the answer. The loop runs once per set bit, at most 32 times. The JavaScript, Java and C++ references instead read the lowest bit (x % 2 or x & 1) and then halve x with division or an unsigned shift (>>> on a masked long in Java, an unsigned 64-bit type in C++). Either way, values above 2^31 - 1 are never sign-extended or read as negative, which is the classic bug when the input is stored in a signed 32-bit int (an arithmetic shift of a negative value never reaches zero). Edge cases: n = 0 skips the loop and returns 0; n = 4294967295 returns 32; n = 2147483648 has only bit 31 set and returns 1.

Time complexity:
O(1)
Space complexity:
O(1)