Reverse Bits

Asked byAppleFacebookQualcommMicrosoftGoogle

Problem

Given a 32-bit unsigned integer, return the integer obtained by reversing the order of its bits.

Examples

Example 1
Input:n = 00000010100101000001111010011100
Output:964176192 (00111001011110000010100101000000)

Constraints

  • The input is a 32-bit unsigned integer.

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Bit Manipulation pattern

Work directly on a number's binary representation using bitwise operators — often turning an O(n) or O(log n) problem into a handful of O(1) operations.

Look for this pattern when

  • The problem talks about bits directly (count set bits, reverse bits, single number among duplicates).
  • You want a constant-time trick instead of a loop for a numeric problem.

Read the full Bit Manipulation guide →

Video walkthroughs

Original problem on LeetCode ↗

More Bit Manipulation problems