Problem overview
The method reverseBits(n) receives a number written in binary with exactly 32 bits, leading zeros included. It reverses the order of those 32 bits and returns the number the reversed bits form. For n = 1, the 32-bit form is 31 zeros and a final 1. Reversed, the 1 moves to the front, giving 2147483648. For n = 43261596 the answer is 964176192.
43261596 : 00000010100101000001111010011100
964176192 : 00111001011110000010100101000000
The statement asks for O(1) time, one step for each of the 32 bits, and O(1) extra space. Both n and the answer can be as large as 4294967295 (2**32 - 1), which is bigger than the largest signed 32-bit integer.
Approach: take bits off one end, push them onto the other
Take the bits of n one at a time from the right end: n & 1 is the last bit, and shifting n one place right drops it so the next bit becomes the last. Meanwhile, build result from the right: shift it one place left, which opens a 0 at the right end, and put the bit you took there with OR.
The first bit taken is pushed left by every later round, so after 32 rounds it has moved 31 places and sits at the front. The second moved 30 places, and so on: each bit ends up in its mirror position.
The loop must run exactly 32 times, even after n has become 0. The remaining rounds push zeros in, and they are what move the early bits to the front. With n = 1, the first round puts the 1 into result, and every later round just shifts it one place further left:
round 0: n = 0, result = 1
round 1: n = 0, result = 2
round 2: n = 0, result = 4
round 3: n = 0, result = 8
After 32 rounds the 1 reaches the front.
Step by step
- Create
resultwithlet, starting at 0. - Loop 32 times with
ifrom 0 whilei < 32. All 32 bits must move, leading zeros included. - Shift and insert:
result = (result << 1) | (n & 1). The shift makes room,n & 1isolates the last bit ofn, and OR copies it into the free place. - Shift
nright withn >>>= 1, dropping the bit just used. The>>>operator fills the left end with 0, readingnas an unsigned 32-bit number, as the problem does. - Return
result >>> 0, the same 32 bits read as an unsigned number.
The solution in JavaScript
class Solution {
/**
* @param {number} n
* @return {number}
*/
reverseBits(n) {
let result = 0;
for (let i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>>= 1;
}
return result >>> 0;
}
}
The JavaScript details are the heart of this lesson:
- Bit operators use signed 32-bit integers.
<<,|and&convert their operands to signed 32-bit integers, and the result is signed too. When the first bit ofresultis 1, the number reads as negative: forn = 1, the loop leavesresultequal to-2147483648. >>> 0reinterprets as unsigned. The unsigned right shift by zero changes no bits, but it produces a non-negative number, so-2147483648 >>> 0is2147483648. Without it,reverseBits(1)would return-2147483648andreverseBits(4294967293)would return-1073741825, instead of 2147483648 and 3221225471.>>>=versus>>=.>>keeps the sign by copying the left bit, while>>>fills with 0. Here the choice of shift onndoesn't change the answer, since 32 bits are read from the right end and the fill only affects bits that are never used, but>>>matches the unsigned reading of the input.- Inputs above 2147483647.
n & 1works on the low 32 bits ofn, so values up to 4294967295 are read correctly. For example,-1 >>> 0is 4294967295. - Compound assignment.
n >>>= 1is shorthand forn = n >>> 1. It changes only the local parameter.
Complexity
Time: O(1). The loop always runs 32 times, whatever n is, with a few bit operations per round.
Space: O(1). Only result, n and the counter are stored.
Edge cases and common mistakes
- Zero.
reverseBits(0)returns0. - All bits set.
reverseBits(4294967295)returns4294967295: the reversed pattern of 32 ones is the same. - Forgetting
>>> 0. This is the usual mistake in JavaScript. Any input whose lowest bit is 1 puts a 1 in the sign bit, and the method returns a negative number. - Stopping when
nis 0. A loop likewhile (n !== 0)skips the leading zeros. Forn = 1it would return 1 instead of 2147483648, because the single bit never travels to the front. - Using
result * 2or+instead of<<and|. They are equivalent in principle, but the lesson's bit operators keep everything in 32 bits and make the intent clear. - Doing the shift in the wrong order. Shift
resultfirst, then insert the bit. Inserting first and shifting afterwards leaves a 0 in the last place and an extra shift overall.
Testing
n = 43261596gives964176192.n = 1gives2147483648.n = 4294967293gives3221225471.n = 0gives0.n = 2gives1073741824.n = 3gives3221225472.n = 4294967295gives4294967295.
