Show problems as
Classic problem #190EasyDivide and ConquerBit Manipulation

Reverse Bits

Real-world version: Punch Card Fed Backwards
Tracked in this browser only
Read Reverse Bits on LeetCode(opens in a new tab)

The 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.

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