2Brute2Best
πŸ”₯0

Reverse Bits

EasyΒ·LeetCode #190 Pro OnlyMedium
LeetCode
01

Problem Statement

Take the 32 bits of n and put them in the opposite order. Bit 0 moves to position 31, bit 1 moves to position 30, and so on. Return the number that the reversed bits form.

Follow-up: if this function is called many times, how would you speed it up?

Input

An integer n, written in 32 bits (leading zeros count).

Output

The integer formed by the 32 reversed bits.

Example 1
Input: n = 43261596
Output: 964176192

In 32 bits, 43261596 is 00000010100101000001111010011100. Reversed, it is 00111001011110000010100101000000, which is 964176192.

Example 2
Input: n = 2147483644
Output: 1073741822

In 32 bits, it is 01111111111111111111111111111100. Reversed, it is 00111111111111111111111111111110, which is 1073741822.

Constraints
  • β€’ 0 ≀ n ≀ 2^31 βˆ’ 2
  • β€’ n is even
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œreverse the bits”
β†’ Bit `i` must move to position `31 βˆ’ i`, so the bits are moved one by one.
β€œ32 bits, signed integer”
β†’ The width is fixed, so exactly 32 steps are right, including the zeros.
β€œcalled many times (follow-up)”
β†’ A lookup table for bytes is the usual speed-up. It is not needed for the main answer.
03

Approach Complexity Ladder

Brute Force β€” reverse the textO(32)O(32)

Write 32 characters of 0 and 1, reverse the string, read it back as a number

Optimal β€” move each bit to its mirror placeO(32)O(1)

Read n's bits from the bottom and place each one at the top of res, for exactly 32 steps

Brute Force Intuition

Turn n into a 32-character string of 0s and 1s, reverse the string, and parse it back as a binary number. Each step is easy to check by eye.

Recognition rule for this sub-pattern:

n & (nβˆ’1) removes the lowest set bit; shifts read bits one at a time.
Remember this Β· Bit Manipulation
Trigger
"Every element appears twice except…", power of two, counting set bits, subsets as bitmasks, O(1) space tricks.
Think
What happens to this problem if I look at each bit separately?
Key question
Which bit operation (XOR, AND, OR, shift) cancels or isolates what I need?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.