Reverse Bits

Medium~15 min

Given a non-negative integer n, reverse all 32 bits of its binary representation and return the resulting integer.

For example, the 32-bit representation of 13 is 00000000000000000000000000001101. Reversing gives 10110000000000000000000000000000, which is 2952790016.

Treat the input as an unsigned 32-bit integer.

Examples

Example 1
Input: n = 13
Output: 2952790016
Explanation: 13 = 00000000000000000000000000001101 reversed = 10110000000000000000000000000000 = 2952790016.
Example 2
Input: n = 43261596
Output: 964176192
Explanation: 43261596 = 00000010100101000001111010011100 reversed = 00111001011110000010100101000000 = 964176192.
Example 3
Input: n = 0
Output: 0
Explanation: All 32 bits are 0. Reversing gives 0.

Constraints

  • 0 <= n <= 2^32 - 1
  • The input is treated as a 32-bit unsigned integer
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run