Reverse Bits
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
13Output
2952790016Example 2
Input
6Output
1610612736Example 3
Input
4294967294Output
2147483647Approach
Do 32 steps: shift the result left and append the input's lowest bit, then shift the input right.
| Pattern | Bit shifting |
|---|---|
| Time | O(32) |
| Space | O(1) |
Watch out for
The result must be unsigned; in JavaScript finish with >>> 0.