Problem overview
The function reverseBits receives a uint32 n, a number written in binary with exactly 32 bits, leading zeros included. It returns the number formed by those 32 bits in reverse order: the last bit becomes the first, the second to last becomes the second, and so on.
For n = 43261596 the 32 bits and their reversal are:
00000010100101000001111010011100
00111001011110000010100101000000
The reversed bits form 964176192. For n = 1, the lone 1 bit moves from the end to the front, and the answer is 2147483648, which is 2 to the power 31. For n = 4294967293 the answer is 3221225471.
The lesson asks for O(1) time, one step per bit of a 32-bit number, 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, and add each to a new number result from its right end too. The first bit taken ends up shifted left by every later round, so it finishes at the far left. The last bit taken is never shifted, so it stays at the far right. That is exactly a reversal.
One round does two things:
- Shift
resultone place left, which leaves a 0 in its last place, and copy the last bit ofn, which isn & 1, into that place with OR. - Shift
none place right, which drops the bit just copied, so the next bit becomes the last one.
The loop must run exactly 32 times, even after n has become 0. The leading zeros of n are bits too: they become the trailing zeros of the answer. Stopping early when n reaches 0 would leave the other bits too far to the right.
For n = 1, the 1 enters result in the first round and is then shifted left 31 more times. The results after rounds 1, 2, 31 and 32 are 1, 2, 1073741824 and 2147483648. For n = 43261596 the first rounds look like this in binary:
round 1 result=0 n=00000001010010100000111101001110
round 2 result=0 n=00000000101001010000011110100111
round 3 result=1 n=00000000010100101000001111010011
round 4 result=11 n=00000000001010010100000111101001
The number 43261596 ends in 00, so the first two bits taken are 0, and then come two 1s.
Step by step
- Declare
resultwithvaras auint32, starting at 0. It must have the same type asn. - Write a loop that runs 32 times:
ifrom 0 whilei < 32. All 32 bits move, leading zeros included. - Shift and insert. Inside the loop, set
resultto(result << 1) | (n & 1). - Drop the bit you used with
n >>= 1. - Return
resultafter the loop.
The solution in Go
func reverseBits(n uint32) uint32 {
var result uint32 = 0
for i := 0; i < 32; i++ {
result = (result << 1) | (n & 1)
n >>= 1
}
return result
}
The Go details worth knowing:
- Why
uint32. The values go up to 4294967295, which does not fit in a signed 32-bit integer.uint32holds 0 to 4294967295 exactly. - Types do not mix. Go combines only numbers of the same type. Adding an
intto auint32is a compile error:invalid operation: count + n (mismatched types int and uint32). Convert explicitly, as inuint32(x), when you need to. - Left shifts discard overflow. In
result << 1, a bit pushed past the 32nd place is dropped silently. The loop never relies on it, because after 32 roundsresultholds exactly 32 bits. - Right shifts on unsigned numbers fill with 0.
n >>= 1brings in a 0 at the left, sonreaches 0 after at most 32 rounds. On a signed negative number, Go would copy the sign bit instead. - Parameters are copies. Changing
ninside the function does not affect the caller's variable.
Complexity
Time: O(1). The loop always runs 32 times, whatever the value of n.
Space: O(1). The function keeps result, n and the loop counter.
Edge cases and common mistakes
- Zero and all ones.
reverseBits(0)is 0, andreverseBits(4294967295)is 4294967295, since every bit is the same. - A single high bit.
reverseBits(2147483648)is 1, the mirror of then = 1example. - Stopping when
nis 0. The leading zeros would never be pushed intoresult, and the answer would come out too small. That is why the loop counts 32 rounds instead of testingn. - Using a different type for
result. The statementresult = (result << 1) | (n & 1)mixesresultwithn, so both must beuint32. Declaringresultas anintis a type mismatch. - Reversing twice.
reverseBits(reverseBits(43261596))returns 43261596 again.
Testing
n = 43261596gives964176192.n = 1gives2147483648.n = 4294967293gives3221225471.n = 0gives0.n = 4294967295gives4294967295.n = 2147483648gives1.
