Skip to content

Reverse Bit Order

Easy#bit-shifting#accumulator~20 min

The mission

Problem

Treat a non-negative whole number as a fixed field of 32 binary digits, padded with zeros on the left. Return the value you get by reversing the order of those 32 digits, so the lowest digit becomes the highest and vice versa. The width is always 32, whatever the size of the input.

Examples

Input:  value = 1
Output: 2147483648
Why:    the single low digit moves all the way to the top of the field
Input:  value = 3
Output: 3221225472
Why:    the two low digits become the two highest ones
Input:  value = 0
Output: 0
Why:    edge case, a field of zeros reads the same in either direction

Hints

0 / 3

Target time
O(1)
Target space
O(1)

Constraints

  • value is a non-negative integer read as 32 bits

Stuck on the idea rather than the code? Binary and Bitwise Ops covers it.

Python 3Ctrl+Enter runs
Custom test case