Problem overview
The method reverseBits(_ n: UInt32) -> UInt32 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 bits are 31 zeros followed by a 1. Reversed, the 1 moves to the front, giving 10000000000000000000000000000000, which is 2147483648. For n = 43261596:
00000010100101000001111010011100
00111001011110000010100101000000
The second line is the first one reversed, and its value is 964176192.
The lesson asks for O(1) time (one step for each of the 32 bits) and O(1) extra space.
Approach: take bits from the right, push them in from the right
Take the bits of n one at a time from its right end. n & 1 is the last bit, and shifting n one place right drops it, so the next bit becomes the last. Build the answer by shifting it one place left, which leaves a 0 at its right end, and putting the bit just taken into that place with OR.
The first bit taken is pushed first, so every later round shifts it one place further left. After 32 rounds it has moved 31 places, to the front. The last bit taken stays at the right end. Each bit ends up in its mirrored position.
The loop must run all 32 times, even after n has become 0. The leading zeros of n are the last bits taken, and they have to be pushed in so that the earlier bits reach the front.
Step by step
- Copy
nintovar num. Swift parameters are constants, and the loop will take bits offnum. - Create
resultas aUInt32starting at 0. Swift combines only numbers of the same type, so it has to matchnum. - Loop 32 times over
0..<32. The counter is unused, so it is named_. - Push a bit in:
result = (result << 1) | (num & 1). The shift opens a 0 at the right end,num & 1isolates the last bit ofnum, and OR copies it in. - Drop the bit just used:
num >>= 1. - Return
result.
The solution in Swift
class Solution {
func reverseBits(_ n: UInt32) -> UInt32 {
var num = n
var result: UInt32 = 0
for _ in 0..<32 {
result = (result << 1) | (num & 1)
num >>= 1
}
return result
}
}
The Swift details worth a closer look:
UInt32. It is an unsigned 32-bit integer holding 0 to 4294967295. Bothnand the answer can exceed the largest signed 32-bit value, 2147483647, soInt32would not hold them. Its fixed width also means the top bit ofresultis really bit 31.- Shifts don't trap.
<<and>>discard the bits that fall off the end instead of crashing, unlike+and*, which trap on overflow. Here nothing useful is discarded: exactly 32 bits are pushed into a 32-bit value. - Same types.
result << 1andnum & 1are bothUInt32, so the|between them type-checks. The literal1adapts toUInt32. _as the loop variable. It tells readers, and the compiler, that the counter isn't used.
Watching the bits move
For num = 6 (binary 110), here are the first four rounds, with each value printed without leading zeros:
round 1: result=0 num=11
round 2: result=1 num=1
round 3: result=11 num=0
round 4: result=110 num=0
The first bit taken is 0, the second is 1, the third is 1 and the fourth is 0, so after four rounds result holds them as 0110 (printed as 110). The remaining 28 rounds keep shifting them left and push in zeros, because num is already 0.
Complexity
Time: O(1). The loop always runs 32 times, whatever n is.
Space: O(1). Only num and result are stored.
Edge cases and common mistakes
- Zero.
reverseBits(0)returns 0. - All ones.
reverseBits(4294967295)returns 4294967295. - The top bit.
reverseBits(2147483648), which has only the first bit set, returns 1. - Stopping when
numis 0. Awhile num != 0loop would skip the leading zeros.reverseBits(1)would return 1 instead of 2147483648. - Using
Intforresult. It doesn't compile next toUInt32operands: Swift needs matching types, or an explicit conversion. - Shifting before taking the bit. The order matters: shift
resultand put the bit in first, and then drop that bit fromnum.
Testing
n = 43261596gives964176192.n = 1gives2147483648.n = 4294967293gives3221225471.n = 0gives0.n = 2147483648gives1.
