Facebook PixelReverse Bits — Coding Practice
Reverse BitsEasy

Reverse Bits

Easy 4.3k60% acceptance
Bit ManipulationDivide and Conquer

Treat the input n as a 32-bit unsigned integer and reverse the order of its 32 bits, returning the resulting unsigned integer.

Because JavaScript bitwise operations are signed, finish by applying >>> 0 so the answer is interpreted as an unsigned 32-bit value (in [0, 2³² − 1]).

Example 1
Input: n = 43261596
Output: 964176192
00000010100101000001111010011100 reversed is 00111001011110000010100101000000 = 964176192.
Example 2
Input: n = 1
Output: 2147483648
The single low bit moves to bit 31.
Example 3
Input: n = 0
Output: 0
Constraints
  • 0 ≤ n ≤ 2³² − 1
  • Interpret n as exactly 32 bits.
Asked atAppleAmazonMicrosoft
JavaScript
Loading editor…
Case 1
43261596
expected: 964176192
Case 2
0
expected: 0
Case 3
1
expected: 2147483648
Case 4
2147483648
expected: 1
Case 5
4294967295
expected: 4294967295
Case 6
4294967293
expected: 3221225471