Problem overview
The method reverseBits takes a uint32_t n, viewed as exactly 32 bits with leading zeros, and returns the number formed by those bits in reverse order: the last bit becomes the first, and so on.
For n = 1, the 32 bits are 31 zeros and a final 1. Reversed, the 1 comes first, and the number is 2147483648, which is 2 to the power 31. For n = 43261596 the answer is 964176192, and for n = 4294967293 it is 3221225471.
The statement asks for O(1) time, one step per each of the 32 bits, and O(1) space.
Approach: take bits from the right, push them in from the right
Read the bits of n from its right end, one at a time: n & 1 is the last bit, and n >>= 1 drops it so the next bit becomes the last. Build the answer in result: each round, shift result one place left, which opens a 0 at its right end, and copy the bit just taken into that place with OR.
The first bit taken enters first, then moves left every round that follows, so after 32 rounds it has moved 31 places, to the front. The last bit taken stays at the right end. That is the reversal.
The loop must always run 32 times, even after n has become 0. Those later rounds push zeros, which shift the earlier bits into their final places. For n = 13, which is 1101, the lesson's code gives these values of result after the first rounds:
round 1 result=1 n=6
round 2 result=2 n=3
round 3 result=5 n=1
round 4 result=11 n=0
round 5 result=22 n=0
round 32 result=2952790016 n=0
After four rounds result is 1011, the reversal of 1101. The 28 remaining rounds append zeros, giving 2952790016, which is 1011 followed by 28 zeros.
Step by step
- Create
resultas auint32_tset to 0. - Loop 32 times with
ifrom 0 to 31. Every bit must move, leading zeros included. - Shift and insert:
result = (result << 1) | (n & 1). The shift makes room;n & 1is the bit to place; OR copies it in. - Shift
nright withn >>= 1, so its next bit becomes the last one. - Return
result.
The solution in C++
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t result = 0;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>= 1;
}
return result;
}
};
C++ details worth knowing:
- Why
uint32_t. It is an unsigned integer of exactly 32 bits, holding 0 to 4,294,967,295. Anintstops at 2,147,483,647, so inputs like 4294967293 and answers like 2147483648 don't fit. - Unsigned shifts are safe. Shifting a
uint32_tleft drops the bits that fall off the left end, with no undefined behaviour. Overflowing a signedintwould be undefined behaviour. - Right shift fills with zeros. On an unsigned value,
n >>= 1brings in a 0 on the left. On a negative signedintit would typically copy the sign bit instead, and the later rounds would not push zeros.
Complexity
Time: O(1). The loop always runs 32 times, whatever the value of n.
Space: O(1). Two numbers, result and n, plus the counter.
Edge cases and common mistakes
- Stopping when
nis 0. That drops the trailing zeros of the answer. Withn = 13, stopping after round 4 would return 11 instead of 2952790016. - Zero and all ones.
0reverses to 0, and4294967295reverses to itself. - Powers of two.
n = 2has its 1 in the second place from the right. It ends second from the left, giving 1073741824. - Using a signed type. An
intcan't hold answers above 2147483647, such as 2147483648 forn = 1.
Testing
n = 43261596gives 964176192.n = 1gives 2147483648.n = 4294967293gives 3221225471.n = 0gives 0.n = 4294967295gives 4294967295.n = 2gives 1073741824.
