Problem overview
The method ReverseBits takes a uint n, read as exactly 32 bits with leading zeros included, and returns the uint formed by those bits in reverse order: the last bit becomes the first, the second to last becomes the second, and so on.
For n = 1, the bits are 31 zeros and a final 1. Reversed, the 1 moves to the front, giving 2147483648 (2 to the power 31). For n = 43261596 the 32-bit form is 00000010100101000001111010011100, and the reversed bits 00111001011110000010100101000000 are 964176192.
Approach: take bits off one end, push them onto the other
Take the bits of n one at a time from its right end. n & 1 is the last bit, and shifting n right by one drops it, so the next bit becomes the last. Meanwhile build result from the right: shift it left by one, which leaves a 0 at its right end, and copy the bit you took into that spot with OR.
The first bit taken goes in first, and each later bit pushes it one place further left. After 32 rounds it has moved 31 places, to the front, which is the mirrored position. Every bit ends in its mirror place.
The loop must run exactly 32 times, even once n has become 0. The leading zeros of n are real bits: they become the last bits of the answer, and the extra shifts of result are what move the early bits all the way to the front.
Step by step
- Create
resultas auintset to 0.uintis C#'s unsigned 32-bit integer, holding 0 to 4,294,967,295 (2 to the power 32, minus 1). That is the range ofnand the answer, where anintstops at 2,147,483,647. - Loop 32 times:
int ifrom 0 whilei < 32. - Update
result:result = (result << 1) | (n & 1);. Shifting left makes room, and OR copies the last bit ofninto it. - Shift
nright:n >>= 1;. This discards the bit just copied, so the next one becomes the last. - Return
result.
For the first example, printing the 32-bit forms of the input and of the method's result gives:
00000010100101000001111010011100
00111001011110000010100101000000
The second line is the first read from right to left, and its value is 964176192.
The solution in C#
class Solution {
public uint ReverseBits(uint n) {
uint result = 0;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>= 1;
}
return result;
}
}
The C# details worth knowing:
- Why
uint. The answer forn = 1is 2147483648, which is larger thanint.MaxValue. With auintthe top bit is an ordinary bit, so the value stays positive. - Right shift on
uintfills with zeros. On anint,>>copies the sign bit, so-1 >> 1is still-1. On auint,>>always brings in 0, which is what the loop needs. - Left shift drops overflowing bits.
result << 1on auintdiscards the top bit if it is set, without an error. Here that can't lose data:resultgets one bit per round, so after 32 rounds it holds exactly 32 bits. n & 1is auint. Both operands areuint(the literal 1 fits), so it can be ORed withresultdirectly, with no cast.nis a copy. Shifting it doesn't change the caller's variable.
Complexity
Time: O(1). The loop always runs 32 times, whatever n is. Space: O(1). Two numbers and the loop counter.
Edge cases and common mistakes
- Zero and all ones.
0returns 0, and4294967295(all 32 bits set) returns 4294967295. - Stopping when
nreaches 0. Forn = 1that would stop after one round withresult = 1instead of 2147483648. The leading zeros must be processed. - Using
int.n = 4294967293doesn't fit in anint, and results above 2147483647 would come out negative. The signature usesuintfor that reason. - Shifting before taking the bit. If you shift
nright first, you lose the bit you meant to take. - Forgetting the parentheses.
(result << 1) | (n & 1)reads clearly, and|has lower precedence than<<and&anyway.
Testing
43261596gives964176192.1gives2147483648.4294967293gives3221225471.0gives0.4294967295gives4294967295.
