Problem overview
The method reverseBits(n: Long): Long treats n as a binary number of exactly 32 bits, leading zeros included, reverses the order of those bits and returns the number the reversed bits form.
For n = 1, the 32 bits are 31 zeros and a final 1. Reversed, the 1 moves to the front: 10000000000000000000000000000000, which is 2 to the power 31, 2147483648. For n = 43261596 the bits are 00000010100101000001111010011100 and the reversed bits 00111001011110000010100101000000 are 964176192.
Approach: pop from one end, push to the other
Take the bits of n from the right, one at a time, and push each one into a new number result from the right. The first bit taken enters first, then every later bit pushes it one place to the left. After 32 rounds, the bit taken first has moved 31 places and sits at the front: the order is reversed.
Each round has three actions:
num & 1reads the last bit ofnum.result << 1shiftsresultone place left, leaving a 0 at its right end, and|(OR) copies the bit into that 0.num >>= 1drops the bit that was just used, so the next one becomes the last.
The loop must run exactly 32 times, even when num has become 0: the leading zeros of the input become the trailing zeros of the answer, and they need their shifts. Doing exactly 32 rounds takes constant time and space.
Step by step
- Copy
nintovar num. A Scala parameter can't be reassigned, and the loop takes bits offnum. - Create
var result = 0L. TheLmakes it aLong. - Loop
for (i <- 0 until 32), once per bit. - Set
result = (result << 1) | (num & 1). - Shift
numright withnum >>= 1. - Return
result.
The solution in Scala
class Solution {
def reverseBits(n: Long): Long = {
var num = n
var result = 0L
for (i <- 0 until 32) {
result = (result << 1) | (num & 1)
num >>= 1
}
return result
}
}
Why Long
An Int is 32 bits and signed, so it tops out at 2147483647. The input can be as large as 4294967295 (one less than 2 to the power 32) and so can the answer, which is why the method uses Long. With an Int, the bit in position 31 is the sign bit: 1 << 31 evaluates to -2147483648, while 1L << 31 is 2147483648.
Scala details
- Both operands of
|areLong:resultis aLong, andnum & 1is aLongtoo, becausenumis. So there are no conversions in the loop. >>is the arithmetic right shift, which copies the sign bit. For these inputsnumis never negative, so it simply inserts zeros on the left. (>>>is the unsigned shift.)num >>= 1is the compound form ofnum = num >> 1.0 until 32runs 32 times, forifrom 0 to 31. The variableiisn't used inside; it only counts.
Testing
Results from the lesson's method:
43261596 -> 964176192
1 -> 2147483648
4294967293 -> 3221225471
0 -> 0
4294967295 -> 4294967295
2 -> 1073741824
The last result shows the mirror effect: 2 is 10 at the right end, and reversed, its 1 lands in the second place from the left, which is 2 to the power 30.
Complexity
Time is O(1): the loop always runs 32 times, whatever n is. Space is O(1): two Longs.
Edge cases and common mistakes
- Stopping early: a loop like
while (num != 0)stops once the input's bits run out and leaves the result too small. Forn = 1with only three rounds the result is 4, not 2147483648. All 32 rounds are needed. - Using Int: results above 2147483647 turn negative or lose bits. Keep the
Longtype in the signature and write the literal as0L. - Shifting before reading: the order is shift
result, OR in the bit, then shiftnum. Shiftingresultafter the OR would move the new bit one place too far, and the answer would be doubled. - Palindromic bit patterns: a number whose bits read the same both ways, such as 4294967295, comes back unchanged. It is a handy sanity check.
