Skip to main content

25. Number of 1 Bits

easyAsked at Salesforce

Count the number of set bits (1s) in a 32-bit unsigned integer. Salesforce uses this to test the Brian Kernighan bit-trick used internally in their permission-mask aggregation.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce permission-set team uses popcount in real production code.
  • LeetCode Discuss (2025)Asked to surface knowledge of Brian Kernighan's algorithm.

Problem

Write a function that takes the binary representation of 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 = 11
Output
3

Explanation: Binary 1011 has three 1 bits.

Example 2

Input
n = 128
Output
1

Example 3

Input
n = 2147483645
Output
30

Approaches

1. Bit-by-bit scan

Loop 32 times; count if LSB is 1; shift right.

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

Tradeoff: Linear in the number of bits. Works but does up to 32 iterations even when few bits are set.

2. Brian Kernighan's trick

Clear the lowest set bit each iteration (n & (n-1)). Iterations = number of set bits.

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

Tradeoff: Faster when few bits are set. The n & (n-1) trick zeros out the lowest set bit in O(1).

Salesforce-specific tips

Salesforce uses popcount in permission masks (each user's permission set is a bitmask; counting permissions = popcount). They specifically grade on knowing the Brian Kernighan trick. Bonus signal: mention that x86 has a POPCNT instruction and modern JS engines exploit it; for truly large counts, that's the optimal answer.

Common mistakes

  • Using >> instead of >>> — infinite loop if the input has bit 31 set (sign extension).
  • Counting up to a fixed 32 — works but slower than Kernighan.
  • Forgetting the n !== 0 termination — works in JS but explicit is clearer.

Follow-up questions

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

  • Counting bits for all numbers 0..n (LC 338).
  • Hamming Distance (LC 461).
  • Find the missing number using XOR (LC 268).

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 flips the lowest set bit to 0 and all lower zeros to 1. ANDing with the original keeps only bits above the lowest set bit, with everything else 0.

When is the simple loop better?

When the input is mostly 1s (e.g., 0xFFFFFFFF). Kernighan does popcount iterations; the simple loop does 32 regardless.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →