Reverse Bits

EasyBit Manipulation

You are given an integer n that represents a 32-bit unsigned integer, between 0 and 2^32 - 1. Reverse the order of its 32 bits, leading zeros included: bit 0 (the lowest) becomes bit 31 (the highest), bit 1 becomes bit 30, and so on.

Return the result as a 32-bit unsigned integer, that is, a number between 0 and 2^32 - 1. A negative answer is not accepted.

Constraints

  • 0 <= n <= 2^32 - 1

Examples

Example 1

Input: n = 6
Output: 1610612736

6 is 110 in its lowest bits, so 00…0110 reversed is 0110 0…0: bits 29 and 30 are set, which is 2^30 + 2^29 = 1610612736.

Example 2

Input: n = 1
Output: 2147483648

Bit 0 moves to bit 31: 2^31 = 2147483648. The answer must be unsigned, not -2147483648.

Example 3

Input: n = 4294967295
Output: 4294967295

All 32 bits are 1, so reversing changes nothing.

JavaScript you'll use: Bitwise operators Numbers, Math and integer tricks

/**
 * @param {number} n
 * @return {number}
 */
function reverseBits(n) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.