Reverse Bits in Python and Java
In this tutorial, we will learn LeetCode #190: Reverse Bits in very simple language. We will understand the idea step by step, see custom examples, and write complete code in Python and Java.
What Is the Reverse Bits Problem?
This problem asks us to solve a common coding interview task using the given input. The goal is to return the correct result without using a slow brute force method.
For this problem, we will use bit manipulation because it gives a clean and optimized solution.
Example input: n = 13 Expected result: Output concept: reverse 000...1101 Explanation: The bits are reversed from left to right.
Beginner-Friendly Idea
The main idea is to avoid trying every possible answer blindly. Instead, we keep useful information while reading the input and use that information to make the next decision.
At each step, ask: “What do I already know, and how does the current value change my answer?”
Using our example: n = 13 Approach: bit manipulation We update variables step by step until we reach: Output concept: reverse 000...1101
Why Do We Use This Approach?
A direct brute force solution is usually easier to think about, but it can become slow when the input is large. The optimized approach keeps only the important state and avoids repeated work.
That is why bit manipulation is useful for this problem.
Step-by-Step Explanation
Let us dry run the algorithm using a custom example.
Step 1 Use this custom example. n = 13 We will solve it using bit manipulation. Step 2 Look at the first important value from the example and create the variables needed by the algorithm. current_state = based on the first value answer = not finished yet Step 3 Move to the next useful value and update the state. The algorithm compares the new value with the old state. If the new value improves the answer, we update the answer. Step 4 Continue this process until all useful values are processed. After processing the example, we get: Output concept: reverse 000...1101 Why? The bits are reversed from left to right.
Important Code Logic
The most important part is updating the algorithm state after reading each useful value. This is where the answer becomes better step by step.
Think like this: old_state = what we knew before current_value = value we are checking now new_state = updated result after using current_value For our example, the final state gives: Output concept: reverse 000...1101
Example 1
Input: n = 13 Output: Output concept: reverse 000...1101 Explanation: The bits are reversed from left to right.
Example 2
Input: n = 10 Output: reverse bits of 000...1010 Explanation: The bits are reversed from right to left.
Python Code
Here is the complete Python solution for LeetCode #190.
class BitReverser:
def reverse_bits(self, n):
result = 0
for _ in range(32):
result = (result << 1) | (n & 1)
n >>= 1
return result
reverser = BitReverser()
print(reverser.reverse_bits(13))
print(reverser.reverse_bits(8))Java Code
Here is the complete Java solution for LeetCode #190.
class BitReverser {
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result = (result << 1) | (n & 1);
n >>>= 1;
}
return result;
}
public static void main(String[] args) {
BitReverser reverser = new BitReverser();
System.out.println(reverser.reverseBits(13));
System.out.println(reverser.reverseBits(8));
}
}Time and Space Complexity
Time Complexity: O(n)
The time complexity depends on how many values the algorithm needs to process and whether it uses sorting, binary search, heap, or traversal.
Space Complexity: O(n)
The extra space is used for the variables or data structures needed by the optimized approach.
Final Summary
LeetCode #190: Reverse Bits becomes easier when we break it into small steps. First understand what the problem asks, then track the important state, dry run with an example, and finally write the code.