Problem overview
The function reverseBits receives a Long named n, a number written in binary with exactly 32 bits, leading zeros included. It reverses the order of those 32 bits, so the last bit becomes the first, the second to last becomes the second, and so on. It returns the number that the reversed bits form, also as a Long.
For n = 43261596 the 32 bits are 00000010100101000001111010011100. Reversed, they are 00111001011110000010100101000000, which is 964176192. For n = 1, the only 1 bit moves from the last place to the first, giving 2147483648, which is 2 to the power of 31.
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, and add each one to the right end of a growing answer. The bit taken first goes in first, and every later bit pushes it one place to the left. After 32 rounds, the first bit taken has moved 31 places and sits at the front, and every other bit is in its mirrored place.
Two operations do the work. num and 1L reads the last bit of num, and shifting num one place right drops that bit so the next one becomes the last. On the answer side, shifting result one place left makes room with a 0 at the right end, and or copies the bit into that 0.
The loop must run exactly 32 times, even after num has become 0. The leading zeros of n are real bits of the 32-bit number, and they become the trailing zeros of the answer. Stopping early would leave the answer shifted too far to the right. With a fixed 32 rounds, the time is O(1).
Step by step
- Copy
nintonum, avar, because Kotlin parameters are read-only. The loop takes bits offnum. - Create
resultas avarstarting at0L. TheLmakes the literal aLong, matching the return type. - Loop
iover0 until 32. Every bit must move, zeros included. - Push one bit into the answer:
result = (result shl 1) or (num and 1L). - Drop that bit from
num:num = num shr 1. - Return
result.
The solution in Kotlin
class Solution {
fun reverseBits(n: Long): Long {
var num = n
var result = 0L
for (i in 0 until 32) {
result = (result shl 1) or (num and 1L)
num = num shr 1
}
return result
}
}
To see the mechanism on a short number, here are the first six rounds for n = 13 (binary 1101), showing the bits of result and num after each round without leading zeros:
round 1: result=1 num=110
round 2: result=10 num=11
round 3: result=101 num=1
round 4: result=1011 num=0
round 5: result=10110 num=0
round 6: result=101100 num=0
The bits of 13 come out in the order 1, 0, 1, 1, and the rounds after that push in zeros. After 32 rounds the first bit taken is the leading bit of the answer.
The Kotlin details worth knowing:
- Why
Long. AnIntholds up to 2147483647, butnand the answer can be as large as 4294967295.1L shl 31is 2147483648, while1 shl 31on anIntis -2147483648. - The
Lsuffix.0Land1LareLongliterals. They keepresultaLong, so it can hold all 32 bits without wrapping into negative numbers. - Bit operators are words:
shl,shr,andandorare infix functions. shris a signed shift. It copies the sign bit. Herenumis never negative, since it is at most 4294967295 in a 64-bitLong, so the shift brings in zeros.ushris the unsigned version, which would give the same result for these values.
Complexity
Time: O(1). The loop always runs 32 times, whatever n is.
Space: O(1). The function keeps two numbers.
Edge cases and common mistakes
- Zero. Every bit is 0, so the answer is 0.
- All ones.
4294967295, which is 32 ones, reverses to itself. - Stopping when
numis 0. Forn = 1the loop would stop after one round and return 1. Running all 32 rounds moves the bit to the front and gives 2147483648. - Using
Int. Values from 2147483648 up don't fit, and the answer forn = 1would turn negative.
Testing
n = 43261596gives964176192.n = 1gives2147483648.n = 4294967293gives3221225471.n = 0gives0.n = 4294967295gives4294967295.
