Skip to main content
Back to problems
Leetcode
Medium
Bit Manipulation
Math
Google
Reverse Bits

Reverse the bits of a 32-bit unsigned integer and return the resulting value.

Acceptance 100%
Problem Statement

Reverse Bits

Given a 32-bit unsigned integer, reverse its bits and return the integer you get after reversing.

Treat the input as a fixed-width 32-bit value. The leftmost bit becomes the rightmost bit, the second leftmost becomes the second rightmost, and so on.

For example, if the input bits are:

clike

00000010100101000001111010011100

then the reversed bits are:

clike

00111001011110000010100101000000

Return the decimal value of the reversed bit pattern.

Input Format

  • A single integer n representing a 32-bit unsigned value.
  • You should consider only its lowest 32 bits.

Output Format

  • Return one integer: the value obtained after reversing the 32 bits of n.

Constraints

  • Interpret the input as a 32-bit unsigned integer.
  • The solution should work for all 32 bit positions.
  • Constant extra space is expected.
Examples
Sample cases returned by the problem API.

Example 1

Input

n = 43261596

Output

964176192

Explanation

The binary form of 43261596 is 00000010100101000001111010011100. Reversing the 32 bits gives 00111001011110000010100101000000, which is 964176192.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.