Reverse Bits of a Number in DSA Java - Time & Space Complexity
We want to understand how the time to reverse bits in a number changes as the number size changes.
Specifically, how many steps does it take to flip all bits from right to left?
Analyze the time complexity of the following code snippet.
public int reverseBits(int n) {
int result = 0;
for (int i = 0; i < 32; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
This code reverses the bits of a 32-bit integer by shifting and masking each bit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs 32 times, once for each bit.
- How many times: Exactly 32 times, fixed for 32-bit integers.
Since the loop runs a fixed 32 times regardless of input value, the steps stay the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (binary 1010) | 32 steps |
| 100 (binary 1100100) | 32 steps |
| 1000 (binary 1111101000) | 32 steps |
Pattern observation: The number of steps does not grow with input size because the bit length is fixed.
Time Complexity: O(1)
This means the time to reverse bits is constant and does not increase with the input number.
[X] Wrong: "The time depends on the value of the number or its size."
[OK] Correct: The code always processes 32 bits, so time is fixed regardless of the number's value.
Understanding fixed-time bit operations helps you handle low-level data tasks confidently in interviews and real projects.
"What if we changed the code to reverse bits of a 64-bit number? How would the time complexity change?"
Practice
Solution
Step 1: Understand bit order
Bits are the 0s and 1s that make up a number. Reversing means flipping their order.Step 2: Explain reversing bits
If the bits are 1010, reversing them gives 0101, flipping left to right.Final Answer:
Flipping the order of bits from left to right -> Option BQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with flipping bits (0 to 1)
- Thinking reversing means adding or subtracting
- Mixing bit shifting with bit reversing
n using a loop?Solution
Step 1: Analyze bit extraction and shifting
for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } extracts the least significant bit (n & 1), shifts result left, then adds the bit, and shifts n right.Step 2: Check correctness of loop operations
for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } correctly reverses bits by building result from left to right while moving through n's bits right to left.Final Answer:
for (int i = 0; i < 32; i++) { result <<= 1; result |= (n & 1); n >>= 1; } -> Option AQuick Check:
Shift result left, add bit from n, shift n right [OK]
- Shifting n left instead of right
- Adding bits without shifting result
- Using incorrect bit extraction
int n = 13; // binary 00000000000000000000000000001101
int result = 0;
for (int i = 0; i < 4; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
System.out.println(result);Solution
Step 1: Trace each loop iteration
Initial n=13 (binary 1101). Loop runs 4 times:
i=0: result=0<<1=0; result|=1 (n&1=1); result=1; n=6
i=1: result=1<<1=2; result|=0 (6&1=0); result=2; n=3
i=2: result=2<<1=4; result|=1 (3&1=1); result=5; n=1
i=3: result=5<<1=10; result|=1 (1&1=1); result=11; n=0Step 2: Convert final result to decimal
Result is 11 decimal after reversing 4 bits of 13.Final Answer:
11 -> Option AQuick Check:
Reversed 1101 bits = 1011 = 11 decimal [OK]
- Not limiting loop to 4 bits
- Mixing bit order in result
- Confusing decimal and binary values
n:int result = 0;
for (int i = 0; i <= 8; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}Solution
Step 1: Check loop range for 8 bits
The loop runs from i=0 to i=8 inclusive, which is 9 iterations, but 8 bits need only 8 iterations.Step 2: Understand effect of extra iteration
Extra iteration shifts result one more time and adds an extra bit, causing incorrect reversed value.Final Answer:
Loop runs one extra time causing wrong result -> Option CQuick Check:
Loop should run 8 times, not 9 [OK]
- Using <= instead of < in loop condition
- Shifting result right instead of left
- Not shifting n inside loop
Solution
Step 1: Consider efficiency and correctness
Looping 16 times, shifting result left and extracting bits from n right is efficient and direct for 16-bit reversal.Step 2: Evaluate other options
Converting to string is slower and error-prone; recursion adds overhead; shifting n left 16 times loses bits.Final Answer:
Use a loop shifting result left and extracting bits from n right, running 16 times -> Option DQuick Check:
Loop + bit shifts = best for fixed bit reversal [OK]
- Using string conversion for bit operations
- Recursion without base case or efficiency
- Shifting n left losing original bits
Ready to prove it?
Pass the test and this pattern moves unknown
