24. Reverse Bits
easyAsked at SalesforceReverse the bits of a given 32-bit unsigned integer. Salesforce uses this to gauge whether candidates can comfortably reason about bit manipulation.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Asked occasionally on Salesforce platform-team phone screens.
- LeetCode Discuss (2025)— Used as a low-level bit-manip filter.
Problem
Reverse bits of a given 32 bits unsigned integer.
Constraints
The input must be a binary string of length 32
Examples
Example 1
n = 00000010100101000001111010011100964176192 (00111001011110000010100101000000)Example 2
n = 111111111111111111111111111111013221225471 (10111111111111111111111111111111)Approaches
1. Convert to binary string and reverse
Convert to 32-char binary string (pad with zeros), reverse, parse back.
- Time
- O(32)
- Space
- O(32)
function reverseBits(n) {
const bin = n.toString(2).padStart(32, '0');
return parseInt([...bin].reverse().join(''), 2);
}Tradeoff: Works but uses string allocation. Salesforce prefers the bit-shift version.
2. Bit-shift loop
Loop 32 times: extract LSB of n, shift result left, OR in the bit, shift n right.
- Time
- O(32)
- Space
- O(1)
function reverseBits(n) {
let result = 0;
for (let i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>>= 1;
}
return result >>> 0;
}Tradeoff: Pure bitwise. The >>> 0 at the end coerces to unsigned 32-bit, which JavaScript needs because bitwise ops use signed 32-bit.
Salesforce-specific tips
Salesforce platform-team uses bit manipulation in their permission masks and field-level security tracking. They grade on the >>> 0 coercion — forgetting it returns a negative number in JS. Bonus signal: mention the bit-block reversal trick (divide-and-conquer that reverses in O(log 32) operations).
Common mistakes
- Using >> instead of >>> — preserves sign bit, giving negative results.
- Forgetting the final >>> 0 — JavaScript bitwise ops return signed int32.
- Reversing only the bits used by n.toString(2) — loses leading zeros.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Reverse bits in O(log n) using divide-and-conquer.
- Number of 1 Bits (LC 191).
- Counting bits 0..n (LC 338).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why do we need >>> 0 at the end?
JavaScript bitwise ops use signed 32-bit. If the top bit of result is 1, it would be interpreted as a negative number. >>> 0 coerces to unsigned.
Is there a faster approach for many calls?
Yes — precompute reversals for each byte and combine. Reduces 32 iterations to 4 lookups. Mention as a real-world optimization.
Practice these live with InterviewChamp.AI
Drill Reverse 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 →