Mirror a 32-Bit Word
EasyBit Manipulation
Problem
A sensor bus sends 32-bit words least-significant bit first, but the receiver reads them most-significant bit first, so every word arrives mirrored.
Given an unsigned 32-bit word, return the unsigned 32-bit value whose bit i equals bit 31 - i of word. The answer must be a non-negative number below 2^32.
Example 1
- Input:
- word = 1
- Output:
- 2147483648
Bit 0 moves to bit 31.
Example 2
- Input:
- word = 6
- Output:
- 1610612736
Bits 1 and 2 move to bits 30 and 29: 0x60000000.
Example 3
- Input:
- word = 0
- Output:
- 0
Constraints
- 0 ≤ word < 2^32 (unsigned 32-bit)
- The result is also an unsigned 32-bit value.
Hint
Show the first hint
Build the answer one bit at a time: which bit of word goes in first?
Walkthrough
How to approach it
01Clarify before coding
- Is the word unsigned? Yes: inputs and outputs are in [0, 2^32), so 2^31 and above are valid and must not come back negative.
- Do leading zeros count? Yes, the width is fixed at 32 bits, so 1 becomes 2^31, not 1.
02Brute force
Format the word as a zero-padded 32-character binary string, reverse it and parse it back.
Cost: O(32) time and O(32) extra space for the string; correct, but it allocates and dodges the bit reasoning the question is about.
The rest of the walkthrough is in the app
The full walkthrough (key insight, step-by-step, dry run, complexity, edge cases, pitfalls, follow-ups) and solutions in Python and JavaScript are in the app, next to an editor and a terminal that runs your code against the tests.
This problem is part of Pro.