Skip to main content

25. Number of 1 Bits

easyAsked at Plaid

Count the number of set bits in an unsigned integer. Plaid asks this as a bitwise warm-up before harder bitmask problems on account-feature flags.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid OA — bitmask intro.
  • LeetCode Discuss (2026)Plaid warm-up.

Problem

Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).

Constraints

  • The input must be a binary string of length 32.

Examples

Example 1

Input
n = 00000000000000000000000000001011
Output
3

Example 2

Input
n = 11111111111111111111111111111101
Output
31

Approaches

1. Bit-by-bit shift

Loop 32 times: check (n & 1), shift right.

Time
O(32)
Space
O(1)
function hammingWeight(n) {
  let count = 0;
  for (let i = 0; i < 32; i++) {
    count += n & 1;
    n >>>= 1;
  }
  return count;
}

Tradeoff: Constant time. The obvious approach — fine but Brian Kernighan's trick is shorter.

2. Brian Kernighan's trick

n &= (n - 1) clears the lowest set bit. Loop until n is 0; the count of iterations is the Hamming weight.

Time
O(set bits)
Space
O(1)
function hammingWeight(n) {
  let count = 0;
  while (n !== 0) {
    n &= (n - 1);
    count++;
  }
  return count;
}

Tradeoff: Loops only as many times as there are set bits. For sparse bitmasks (typical for feature flags), this is much faster than 32 iterations.

Plaid-specific tips

Plaid loves Brian Kernighan's trick because it shows you've moved past 'iterate every bit position.' Bonus signal: explain why n & (n-1) clears the lowest set bit — borrowing flips all trailing zeros to 1 and the lowest 1 to 0; AND wipes the trailing 1s back to 0 and the original 1 stays cleared.

Common mistakes

  • Using n > 0 instead of n !== 0 — in some languages, n could be interpreted as negative.
  • Using >> instead of >>> in the shift version — sign-extension breaks the loop for large unsigned values.
  • Returning a boolean instead of a count.

Follow-up questions

An interviewer at Plaid may pivot to one of these next:

  • Count set bits for all numbers from 0 to n (LC 338) — DP relation.
  • Hamming distance between two integers (LC 461).
  • Reverse bits (LC 190).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why does n &= n-1 clear the lowest set bit?

Subtracting 1 from n flips the trailing zero run and the lowest 1. AND-ing with n preserves higher bits and clears that lowest 1 (along with the trailing-zero-now-flipped-to-1s).

When is Kernighan slower than 32-iter?

When the integer has many bits set (close to 32). For >24 set bits the difference is noise; for <12 set bits Kernighan wins clearly.

Practice these live with InterviewChamp.AI

Drill Number of 1 Bits and other Plaid interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →