const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Reverse Bits LeetCode Solution: The Bit-by-Bit Technique

Reverse Bits (#190) teaches the extract-and-place pattern — process one bit at a time, build the result from scratch. This is the fundamental technique behind every bit manipulation problem.

6 min read|

3 operations, 32 iterations, every bit reversed

Extract with n & 1, place with result << 1 | bit, advance with n >> 1

Reverse Bits LeetCode #190: The Extract-and-Place Pattern

Reverse Bits (#190) is the purest bit manipulation problem on LeetCode. There are no clever tricks, no mathematical shortcuts — just a clean loop that extracts one bit at a time from the input and places it into the correct position in the result. If you can solve this problem, you understand the fundamental operation behind every reverse bits leetcode question.

The algorithm is elegant in its simplicity. Extract the rightmost bit of n using n & 1, shift the result left to make room, OR the extracted bit into the result, then shift n right to expose the next bit. Repeat 32 times and you have reversed every bit in the integer.

In this walkthrough, you will understand exactly how this bit reversal algorithm works, trace through a concrete binary example, and see why this same extract-and-place pattern appears in related problems like Number of 1 Bits (#191).

Understanding the Reverse Bits Problem

Given a 32-bit unsigned integer, reverse its bits and return the result. For example, the input 00000010100101000001111010011100 (which is 43261596 in decimal) becomes 00111001011110000010100101000000 (which is 964176192 in decimal). Every bit flips to its mirror position across the 32-bit boundary.

This is not the same as flipping bits (changing 0s to 1s). Reversing bits means the bit at position 0 moves to position 31, position 1 moves to position 30, and so on — exactly like reversing an array, but operating on individual bits inside an integer.

The problem appears frequently in hardware and embedded systems interviews at companies like Nvidia and AMD, where bit-level operations are daily work. It also pairs naturally with Number of 1 Bits (#191) since both require you to process a 32-bit integer one bit at a time.

ℹ️

Interview Pair

Reverse Bits (#190) and Number of 1 Bits (#191) are twin problems that appear together at Nvidia and AMD — both use the same bit-by-bit extraction technique.

The Approach: Extract, Shift, Place

The bit reversal algorithm uses three operations per iteration. First, extract the last bit of n with n & 1. This gives you either 0 or 1 — the value of the rightmost bit. Second, make room in the result by shifting it left one position (result << 1), then OR the extracted bit into the result. Third, shift n right by one (n >> 1) to expose the next bit.

You repeat this exactly 32 times because the input is a 32-bit unsigned integer. After 32 iterations, every bit from n has been extracted (rightmost first) and placed into the result (leftmost first), achieving the reversal. The first bit extracted from n ends up in the most significant position of the result.

The time complexity is O(1) because you always perform exactly 32 iterations regardless of the input. The space complexity is also O(1) — you only need one variable for the result. This is as efficient as bit reversal gets for a single integer.

Implementation: The 32-Iteration Loop

The implementation is refreshingly short. Initialize result to 0. Loop 32 times. In each iteration, shift result left by 1, OR in the last bit of n, then shift n right by 1. After the loop, return result. That is the entire leetcode 190 solution.

In pseudocode: result = 0. For i from 0 to 31: result = (result << 1) | (n & 1), then n >>= 1. Return result. Every language implements this identically because the operations are universal across C++, Java, Python, and JavaScript.

One subtlety: the order matters. You must shift result left before OR-ing in the new bit. If you OR first and then shift, you will have an extra shift at the end that pushes your bits one position too far. The sequence is always shift-result, extract-bit, OR-together, advance-input.

  • Initialize result = 0
  • Loop 32 times: result = (result << 1) | (n & 1), then n >>= 1
  • Return result after all 32 iterations
  • O(1) time — fixed 32 iterations regardless of input
  • O(1) space — only one extra variable
💡

Pro Tip

The pattern is 3 operations per iteration: extract last bit (n & 1), place it in result (result << 1 | bit), advance input (n >> 1). 32 iterations, done.

Visual Walkthrough: Reversing a 32-Bit Integer

Let us trace the first few iterations for input n = 00000010100101000001111010011100 (43261596). We start with result = 0.

Iteration 1: n & 1 = 0 (last bit of input is 0). result = (0 << 1) | 0 = 0. n >>= 1 removes that last bit. Iteration 2: n & 1 = 0. result = (0 << 1) | 0 = 0. Iteration 3: n & 1 = 1. result = (0 << 1) | 1 = 1. Now result has its first set bit. Iteration 4: n & 1 = 1. result = (1 << 1) | 1 = 3 (binary 11).

After all 32 iterations, every bit from the input has been extracted from the right side and placed on the left side of the result. The output is 00111001011110000010100101000000 (964176192). You can verify by comparing the input and output strings — they are exact mirror images.

The pattern is mechanical. Each iteration peels off the rightmost bit of n and appends it to the growing result. Because we extract from the right and build from the left, the first extracted bit ends up in position 31 of the result, achieving a complete reversal.

  1. 1Input: 00000010100101000001111010011100 (n = 43261596)
  2. 2Iterations 1-2: extract 0, 0 from right. result = 0
  3. 3Iteration 3: extract 1. result = 1 (binary: ...001)
  4. 4Iteration 4: extract 1. result = 3 (binary: ...011)
  5. 5Iteration 5: extract 1. result = 7 (binary: ...0111)
  6. 6Continue for all 32 bits...
  7. 7Output: 00111001011110000010100101000000 (964176192)

Edge Cases for Reverse Bits

The most obvious edge case is all zeros: input 00000000000000000000000000000000 gives output 00000000000000000000000000000000. Every bit is 0, so reversing changes nothing. This confirms your loop handles the trivial case correctly.

All ones is equally straightforward: 11111111111111111111111111111111 reversed is still 11111111111111111111111111111111. The mirror image of all-ones is identical. Both of these cases verify your boundary conditions without requiring any special handling in the code.

A single bit set is more revealing. Input 00000000000000000000000000000001 (just bit 0 set) should produce 10000000000000000000000000000000 (bit 31 set). This tests that your loop runs the full 32 iterations and places the extracted bit at the correct far end. Alternating bit patterns like 10101010... provide another good sanity check.

  • All zeros (0): reversed is still 0
  • All ones (4294967295): reversed is still 4294967295
  • Single bit at position 0: reversed places it at position 31
  • Alternating bits (0xAAAAAAAA): reversed gives 0x55555555
⚠️

Language Caveat

In Python, integers have arbitrary precision — you may need to handle the 32-bit constraint explicitly. In languages with fixed-width integers (Java, C++), the 32 iterations handle it naturally.

What Reverse Bits Teaches About Bit Manipulation

Reverse Bits (#190) teaches you the most fundamental skill in bit manipulation: extracting a single bit and placing it somewhere else. This extract-and-place pattern is the building block for Number of 1 Bits (#191), which uses the same n & 1 extraction to count set bits instead of placing them.

The problem also reinforces the idea that bit manipulation operates on fixed-width integers. You loop exactly 32 times because the problem specifies a 32-bit unsigned integer. There is no early termination, no variable-length input — the constraint is built into the data type itself.

Practice this alongside other bit manipulation problems like Single Number (#136), Power of Two (#231), and Missing Number (#268) to build an intuition for when bitwise operations replace arithmetic. Review with YeetCode flashcards to make the extract-shift-place loop automatic recall for interview day.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now