Skip to content
Decoder

Reverse Bits

Easy

Problem

Treat n as an unsigned 32-bit integer and reverse the order of its 32 bits: bit 0 swaps with bit 31, bit 1 with bit 30, and so on. Return the result, also as an unsigned 32-bit value (between 0 and 2^32 - 1).

Example 1

Input
n = 43261596
Output
964176192

Example 2

    00000010100101000001111010011100

Example 3

 →  00111001011110000010100101000000

Example 4

Input
n = 1
Output
2147483648
Why
only the top bit is set

Example 5

Input
n = 4294967293
Output
3221225471

Constraints

  • 0 <= n <= 2^32 - 1
  • Always reverse all 32 bits, including leading zeros.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.