Example 1
- Input
- n = 00000010100101000001111010011100
- Output
- 964176192
The input bits represent 43261596. Mirroring all 32 positions gives 00111001011110000010100101000000, which represents 964176192.
Reverse the order of all 32 bits of an unsigned integer, turning the leftmost bit into the rightmost and vice versa. The mirrored bit string is returned as the unsigned integer it represents.
Reverse the bits of a given 32-bit unsigned integer n. Every one of the 32 positions moves to its mirror image, so the leftmost bit becomes the rightmost and the rightmost becomes the leftmost, including leading zeros.
In languages without an unsigned type such as Java, the input and output travel as signed int values. The underlying bit pattern is identical either way, so the reversal works on the raw 32 bits and the result is interpreted as unsigned.
The input bits represent 43261596. Mirroring all 32 positions gives 00111001011110000010100101000000, which represents 964176192.
The input bits represent 4294967293. The mirrored string is 10111111111111111111111111111111, which represents 3221225471.
All 32 positions contain zero, so reflecting their positions still produces 32 zeros.
The only set bit moves from position 0 to position 31. The result is unsigned 2147483648; Java carries that same pattern as signed int -2147483648.
Reversal changes positions, not bit values: input position 0 must reach output position 31, and position 31 must reach position 0. For 43261596, keep the six leading zeros as part of the 32-bit input; they become six trailing zeros in 964176192. Either reverse a padded string or read the lowest bit repeatedly while shifting the answer left. The width is fixed even when the remaining input becomes zero.
Reveal String Conversion: intuition, complexity, and code
n & 1 exposes the current lowest bit.|.0 runs all 32 rounds and returns 0 instead of stopping early.4294967295 with all bits set mirrors to itself and must return 4294967295.n reaches 0, which drops the leading zeros that still need mirroring.>>> 0, which can print a negative number.Reading bits lowest-first with n & 1 plus an unsigned right shift turns any fixed-width integer into a stream you can rebuild in a new order.