Reverse Bits
Real-world version: Punch Card Fed BackwardsThe statement here is our own: the same inputs, outputs and technique.
A jacquard loom is driven by cards carrying a row of exactly 32 punch positions, numbered 0 through 31. A card is handed to you as the integer card: position i is punched precisely when bit i of card is 1, with position 0 the least significant bit. Positions above the highest punched one are simply unpunched, and they are still part of the row.
An operator threads one card in end for end. Return the integer that encodes the row of 32 positions turned around, so that position i of the answer is punched precisely when position 31 - i of card is punched.
Position 0 of card is always unpunched, so position 31 of the answer is always unpunched too and the answer fits in a signed 32-bit integer.
Examples
Example 1
- Input
- card = 6
- Output
- 1610612736
Positions 1 and 2 are punched and the other 30 are not. Turning the row around moves them to positions 30 and 29, and 2^30 + 2^29 is 1610612736.
Example 2
- Input
- card = 2147483646
- Output
- 2147483646
Positions 1 through 30 are punched while positions 0 and 31 are not. Turning the row around sends position 1 to position 30 and position 30 to position 1, so the same positions end up punched and the integer is unchanged.
Example 3
- Input
- card = 4
- Output
- 536870912
Only position 2 is punched, and turning the row around sends it to position 29, giving 2^29.
Constraints
- 0 <= card <= 2^31 - 2
- card is even.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def reversed_punch_card(card: int) -> int:public int reversedPunchCard(int card)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 8 test cases: 8 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's