Problem overview
The function reverse_bits receives a u32, n, written with exactly 32 bits, leading zeros included. It reverses the order of those 32 bits: 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.
For n = 43261596 the 32-bit form is 00000010100101000001111010011100. Reversed, it is 00111001011110000010100101000000, which is 964176192. For n = 1 the single 1 bit moves from the last place to the first, giving 2147483648, which is 2^31. For n = 4294967293 the answer is 3221225471.
Approach: take bits off one end, push them onto the other
Treat n as a stack of bits. The last bit of n is n & 1, and a right shift n >> 1 discards it and brings the next bit into last place. The answer is built the opposite way: a left shift result << 1 makes room at the right end, and OR puts the new bit there. The first bit taken from n is then shifted left by every later round, so after 32 rounds it sits at the far left. The last bit taken stays at the right. That is a reversal.
The loop has to run exactly 32 times, not "until n is 0". Leading zeros of n are bits too, and they must become the trailing zeros of the answer. For n = 1, the first round puts a 1 at the right end of result and the 31 later rounds shift it left, one place per round, until it reaches the left end, which is the value 2147483648.
Step by step
- Copy
ninto a mutablenum. Function parameters are immutable in Rust, and the loop consumes the bits ofnum. - Create
resultas au32starting at 0. It has to be the same type asnum, because Rust combines only integers of one type. - Loop 32 times with
for _ in 0..32. The counter isn't used, so the underscore tells the compiler and the reader that it's deliberately ignored. - Move one bit across. Shift
resultleft by one, then OR in the last bit ofnum. - Drop the bit just copied with
num >>= 1, so the next bit becomes the last one. - Return
resultas the last expression, without a semicolon.
The solution in Rust
impl Solution {
fn reverse_bits(n: u32) -> u32 {
let mut num = n;
let mut result: u32 = 0;
for _ in 0..32 {
result = (result << 1) | (num & 1);
num >>= 1;
}
result
}
}
Rust details worth knowing:
- Why
u32. The answer forn = 1is 2147483648, which is larger than the biggesti32. An unsigned 32-bit integer holds 0 to 4294967295 and has exactly the 32 bits the problem talks about. - Right shifts differ by type. On an unsigned integer,
>>fills the left end with 0s. On a signed integer it copies the sign bit, so-8i32 >> 1is-4. Withu32, the bits that arrive from the left are always 0, which is what this loop needs. - Bits that shift out are lost without a panic. A left shift by one place discards the top bit instead of failing. A shift by 32 or more places is different: it panics in a debug build, and the shifts here are always by 1.
(result << 1) | (num & 1). The parentheses aren't required, since shifts bind tighter than&and&tighter than|, but they make the two halves of the step obvious.for _ in 0..32. The range holds the numbers 0 to 31, so the body runs 32 times. The standard library also offersn.reverse_bits(), which does the whole job for au32.
Complexity
Time: O(1). The loop always runs 32 times, whatever n is.
Space: O(1). The function keeps two integers.
Edge cases and common mistakes
- Zero and all ones.
0returns0, and4294967295returns4294967295, since a reversed run of 1s is the same run. - Stopping when
numis 0. A loop likewhile num != 0skips the leading zeros. Forn = 1it would run once and return 1 instead of 2147483648. - Using
i32. Inputs above 2147483647 don't exist in ani32, so the signature usesu32for bothnand the result. - Shifting in the wrong order. If
resultis shifted after the bit is added, the first bit is shifted 32 times instead of 31, so it falls off the left end and is lost.
Testing
n = 43261596gives964176192.n = 1gives2147483648.n = 4294967293gives3221225471.n = 0gives0.n = 2147483648gives1.
