25. Number of 1 Bits
easyAsked at SalesforceCount 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
n = 113Explanation: Binary 1011 has three 1 bits.
Example 2
n = 1281Example 3
n = 214748364530Approaches
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.
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 →