Reverse Bits
Reverse the bits of a 32-bit unsigned integer
DSA, Easy, Bit Manipulation
Which structure or algorithm, and what does it cost.
A 32-bit register holds the whole number n. Bit 0 is the lowest digit and bit 31 is the highest, and leading zeros count as bits. Flip the register end to end. The bit at position 0 moves to position 31, position 1 moves to 30, and so on. Return the new register as a whole number with no sign.
Examples
Constraints
nis a whole number from 0 to 4,294,967,295.- Always treat
nas exactly 32 bits, even when its top bits are zeros.
Your answer must contain
- The simple method and its cost; a method that meets the target cost and why it is correct, including leading zeros; code; time and memory cost; a hand trace of example 1.
Target Aim for a fixed 32 rounds, which is O(1) time, and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Picture the 32 bits as a row. Ask where each bit lands, and how to build the new row one bit at a time.
Hint 2 the approach
Peel bits off the low end of n, and add each one to the low end of the result. Decide what must happen to the bits already in the result.
Hint 3 the key step
Each round, make room at the low end of the result, then move one bit across from n. How many rounds does a 32-bit register need for a small n?
Walkthrough
Full walkthrough brute force to best
Brute force
Write n as a text of exactly 32 binary digits, reverse the text, and read it back as a number. This is correct. Time is a fixed 32 digits, so it is O(1), but it builds strings and hides the bit idea, and memory is a 32-character text.
Better method
Place each bit directly: for each position i from 0 to 31, if bit i of n is set, set bit 31 minus i of the result. This is also a fixed 32 rounds.
Swap blocks with masks: the two 16-bit halves, then the bytes, then groups of 4, 2 and 1 bits. Five steps, with no loop.
Best method
Build the result one bit at a time. Each round takes the lowest bit of n and puts it at the low end of the result, after moving the bits already in the result one place left. The first bit taken is shifted left in every later round, so it ends highest. After 32 rounds the bit that began at position 0 has been shifted left 31 times, so it sits at position 31, bit 1 sits at 30, and so on. Run exactly 32 rounds so that the leading zeros of a small n still travel to the other end.
Hand trace
Run on 6, which is 110. Round 1: the lowest bit is 0, so the result is 0. Round 2: the lowest bit is 1, so the result is 1. Round 3: the lowest bit is 1, so the result is 11. Round 4: the lowest bit is 0 and n is now 0, so the result is 110, which is 6. Rounds 5 to 32 only shift the result left, 28 times, giving 110 followed by 28 zeros. That is 6 times 2^28, which is 1610612736.
Code
def reverse_bits(n):
result = 0
for _ in range(32):
result = (result << 1) | (n & 1)
n >>= 1
return resultTime and memory
A fixed 32 rounds, so O(1) time, with a few bit operations in each. O(1) memory. A wider register would cost one round per bit.
Common mistakes
- Stopping the loop when n becomes 0. That drops the leading zeros. For n = 1 the answer is 2147483648, not 1.
- Shifting the result left after adding the bit instead of before. Every bit then moves one place too far, and in Python the result grows past 32 bits.
- Reversing the decimal digits of n instead of its bits.