Skip to content
AI360Xpert
Beta
Difficulty: EasyBit Manipulation

Number of 1 Bits

Problem in Plain English

Count the 1 bits in the 32-bit binary form of an unsigned integer, treating a signed input with the same bit pattern identically.

Problem Statement

Given an integer representing a 32-bit unsigned bit pattern, return the number of one bits, called its Hamming weight. The shown inputs are binary representations for readability; the functions receive integers, not strings.

This page retains the generalized unsigned 32-bit form of the problem, including zero and patterns with the high bit set. A signed Java int may carry the same pattern, such as -3 for unsigned 4294967293. The current positive-integer version with 1 <= n <= 2147483647 is a subset of this contract.

Constraints

  • The functions receive one integer carrying a 32-bit pattern, not a binary string.
  • 0≤n≤232−10 \le n \le 2^{32}-1 for the unsigned numeric representation.
  • Java uses the equivalent signed int carrier; the snippets also handle signed 32-bit carriers in Python and JavaScript.

Examples

Example 1

Input
n = 00000000000000000000000000001011
Output
3

The binary form of 11 holds three 1 bits.

Example 2

Input
n = 00000000000000000000000010000000
Output
1

The binary form of 128 holds a single 1 bit.

Example 3

Input
n = 11111111111111111111111111111101
Output
31

This 32-bit pattern holds thirty-one 1 bits (it reads as -3 as a signed int).

Example 4

Input
n = 00000000000000000000000000000000
Output
0

No bit is set. The fixed scan adds zero 32 times; the erasure approach returns immediately without any iteration.

Intuition

A set bit is simply a binary digit equal to one; leading zeros contribute nothing. Example 1 is decimal 11, whose binary form 1011 has three ones. Inspecting each of 32 positions counts them directly. Alternatively, subtracting one and ANDing with the old pattern erases one set bit at a time, so counting erasures skips zero positions. Signed and unsigned carriers must describe the same fixed 32-bit pattern.

Approaches

Scan All 32 Bits

Solution Details

Reveal Scan All 32 Bits: intuition, complexity, and code

Hints

Hint 1
Look at the lowest bit first: n & 1 is 1 exactly when the current last bit is set.
Hint 2
After testing a bit, shift it away with an unsigned right shift so the next bit slides into place.
Hint 3
To skip zeros entirely, erase the lowest set bit with n & (n - 1) and count erasures until nothing remains.

Edge Cases

  • Zero returns 0: the erasure loop performs no work.
  • All 32 bits set, unsigned 4294967295 or signed -1, returns 32.
  • Only the highest bit set, unsigned 2147483648 or signed -2147483648, returns 1. Java subtraction wraps safely within its fixed-width pattern.
  • Unsigned 4294967293 or signed -3 returns 31; Python must mask signed carriers before its erasure loop.

Common Mistakes and Interview Tips

  • Using while (n > 0) with a signed Java carrier skips patterns whose high bit is set. Use n != 0 for bit clearing.
  • Using arithmetic right shift in a loop that waits for zero keeps adding leading ones for negative carriers. The fixed 32-round scan still terminates, but logical shifting makes its zero-fill invariant explicit.
  • In Python, clearing bits of a negative integer without first masking to 32 bits never exhausts its infinitely sign-extended representation.
  • Passing a binary string to these integer signatures, or treating decimal digits as bits, changes the input contract.
  • Interview tip: state the fixed width before claiming constant time; erasing bits takes one iteration per one, while scanning takes exactly 32.

Key Takeaway

n & (n - 1) always drops the lowest set bit — the single most useful bit-manipulation identity for counting, clearing, and power-of-two tests.