Problem overview
The method reverseBits receives a number n that is to be read as a binary number of exactly 32 bits, leading zeros included. It reverses the order of those 32 bits, so the last bit becomes the first, and returns the number the reversed bits form.
For n = 43261596 the 32 bits are 00000010100101000001111010011100. Reversed they are 00111001011110000010100101000000, and the method returns 964176192. For n = 1 the single 1 bit moves from the end to the front, and the result is 2147483648. For n = 4294967293, which is thirty-one 1s with a 0 in second-to-last place, the result is 3221225471.
The input and the answer can be as large as 4,294,967,295. The note asks for O(1) time, one step for each of the 32 bits, and O(1) extra space.
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 that the next bit becomes the last. Meanwhile, build the answer in result by shifting it one place left, which leaves a 0 at its right end, and copying the bit taken from n into that place with OR.
The first bit taken is pushed first, so every later bit pushes it one place further left. After 32 rounds it has moved 31 places, to the front, and each bit sits in its mirrored position. The loop must run all 32 times, even once n has become 0: the leading zeros of n become the trailing zeros of the answer, and skipping those rounds would leave the other bits too far right.
The same idea on a 4-bit input, n = 11 (1011), shows how it builds up:
round result n
0 1 5
1 3 2
2 6 1
3 13 0
The result, 13, is 1101, which is 1011 reversed. For this lesson the loop runs 32 times instead of 4.
Step by step
- Start
resultat 0, declared withlet. - Loop 32 times with
ifrom 0 to 31. Each round moves one bit. - Move a bit.
(result << 1) | (n & 1)shiftsresultleft, takes the last bit ofnand ORs it into the free place. - Drop that bit from
nwithn >>>= 1, so the next bit becomes the last one. The>>>operator fills the left end with 0 and readsnas an unsigned 32-bit number, as this problem does. - Return
result >>> 0. This reads the 32 bits as an unsigned number.
The solution in TypeScript
class Solution {
reverseBits(n: number): number {
let result = 0;
for (let i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>>= 1;
}
return result >>> 0;
}
}
Some TypeScript details matter a lot here:
- Bitwise operators work on signed 32-bit integers.
numberis a double, but<<,&and|first convert their operands to signed 32-bit integers and give back a signed result. When the first bit is 1, the value reads as negative:1 << 31is-2147483648. >>> 0turns the result back into an unsigned number. It keeps the same 32 bits and reads them as a number from 0 to 4,294,967,295. For example,-1 >>> 0is4294967295and(1 << 31) >>> 0is2147483648. Without it, the method returns-2147483648forn = 1, while43261596still gives964176192because its first reversed bit is 0.>>>versus>>.>>keeps the sign bit and copies it in on the left, while>>>always brings in 0. Withn = 2147483648,n >> 1is-1073741824andn >>> 1is1073741824. The input can be above 2,147,483,647, so>>>=is what makes the loop correct.- The input needs no conversion.
n & 1works fornup to 4,294,967,295, because the 32-bit conversion keeps the lowest 32 bits, and the lowest bit is what we read. - Types are erased.
numbersays nothing about 32 bits. TypeScript has no separate unsigned integer type, so the shift trick is how an unsigned 32-bit value is represented.
Complexity
Time: O(1). The loop always runs 32 times, whatever n is, and each round does a constant amount of work.
Space: O(1). The method keeps result and the loop counter, and reuses the parameter n.
Edge cases and common mistakes
- Zero.
reverseBits(0)returns0. - All ones.
reverseBits(4294967295)returns4294967295: the reversal changes nothing. - Stopping early. A loop like
while (n !== 0)skips the leading zeros. Forn = 1it would run once and return1instead of2147483648. - Forgetting
>>> 0. Without it, any answer whose first bit is 1, such as2147483648, comes out negative. Forn = 1the result is-2147483648. - Using
>>=onn. For an input of2147483648or more, the sign bit is copied in on every shift and the later bits come out wrong.
Testing
n = 43261596gives964176192.n = 1gives2147483648.n = 4294967293gives3221225471.n = 0gives0.n = 4294967295gives4294967295.n = 2147483648gives1.n = 2gives1073741824.
