Blind 75 · #73 · Bit Manipulation

Reverse Bits

EasyBit shiftingTime O(32)Space O(1)

Reverse Bits is an easy Bit Manipulation problem from the Blind 75. The key pattern is bit shifting, and a good solution runs in O(32) time.

Problem

Reverse all 32 bits of the unsigned input and print the resulting unsigned integer.

Examples

Example 1

Input

13

Output

2952790016

Example 2

Input

6

Output

1610612736

Example 3

Input

4294967294

Output

2147483647

Approach

Do 32 steps: shift the result left and append the input's lowest bit, then shift the input right.

PatternBit shifting
TimeO(32)
SpaceO(1)

Watch out for

The result must be unsigned; in JavaScript finish with >>> 0.

More Bit Manipulation problems