All problems
0274EasyDivide and ConquerBit Manipulation

Punch Card Fed Backwards

Tracked in this browser only
Write code

Trains the technique from

LeetCode 190Reverse Bits

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def reversed_punch_card(card: int) -> int:
Java
public int reversedPunchCard(int card)
September 7
Apply