Reverse Bits of a Number in DSA C# - Time & Space Complexity
We want to understand how the time needed to reverse bits changes as the number size grows.
How does the number of steps grow when we reverse bits of a number?
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 combining bits one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs 32 times, shifting and combining bits each time.
- How many times: Exactly 32 times, once for each bit in the integer.
Since the loop always runs 32 times, the number of operations stays the same no matter the input number.
| Input Size (bits) | Approx. Operations |
|---|---|
| 10 | 32 |
| 100 | 32 |
| 1000 | 32 |
Pattern observation: The operations count 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 change with the input number.
[X] Wrong: "The time to reverse bits grows with the value of the number."
[OK] Correct: The loop runs a fixed number of times (32), regardless of the number's value.
Understanding fixed-time operations like bit reversal helps you explain efficiency clearly and confidently in interviews.
"What if we changed the input to a 64-bit number? How would the time complexity change?"
Practice
Solution
Step 1: Understand bit order
Bits are arranged from left (most significant) to right (least significant).Step 2: Reverse means flip order
Reversing bits means the leftmost bit becomes rightmost and vice versa.Final Answer:
Flipping the order of bits from left to right -> Option DQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with bitwise NOT (inverting bits)
- Thinking reversing means shifting bits
- Assuming reversing adds or subtracts from the number
n?Solution
Step 1: Analyze the loop logic in int reversed = 0; for(int i=0; i<4; i++) { reversed <<= 1; reversed |= (n & 1); n >>= 1; }
It shifts reversed left by 1, adds least bit of n, then shifts n right by 1, effectively reversing bits.Step 2: Check other options
int reversed = 0; for(int i=0; i<4; i++) { reversed |= (n & 1) << i; n >>= 1; } does not reverse bits, it copies bits in same order. int reversed = n << 4; shifts bits left, not reverse. int reversed = ~n; inverts bits, not reverse.Final Answer:
Option A code correctly reverses bits -> Option AQuick Check:
Shift left + OR + shift right = reverse bits [OK]
- Using bitwise NOT (~) instead of reversing bits
- Shifting bits without combining properly
- Not updating reversed variable inside loop
int n = 13; // binary 1101
int reversed = 0;
for (int i = 0; i < 4; i++) {
reversed <<= 1;
reversed |= (n & 1);
n >>= 1;
}
Console.WriteLine(reversed);Solution
Step 1: Convert 13 to 4-bit binary
13 in binary (4 bits) is 1101.Step 2: Reverse bits step-by-step
Bits: 1 1 0 1 reversed: 1 0 1 1 which is binary 1011 = decimal 11.Final Answer:
11 -> Option AQuick Check:
1101 reversed = 1011 = 11 [OK]
- Not limiting to 4 bits, causing wrong output
- Confusing bit order when reversing
- Assuming reversed number equals original
int ReverseBits(int n) {
int reversed = 0;
for (int i = 0; i < 8; i++) {
reversed |= (n & 1) << i;
n >>= 1;
}
return reversed;
}Solution
Step 1: Analyze bit placement in loop
The code places bits at increasing positions without reversing order.Step 2: Understand reversing bits logic
Reversing requires shifting reversed left and OR with last bit, not shifting bits by i.Final Answer:
The bits are not reversed; they are copied in the same order -> Option CQuick Check:
Shifting bits by i copies order, not reverse [OK]
- Using bit shift by loop index instead of shifting reversed
- Incorrect loop count for bit size
- Initializing reversed incorrectly
Solution
Step 1: Reverse bits correctly
Use loop to shift reversed left, OR with last bit of original, shift original right.Step 2: Check palindrome by comparing reversed and original
If reversed equals original, bits form a palindrome.Final Answer:
Reverse bits using a loop shifting reversed left and OR with last bit, then compare reversed with original -> Option BQuick Check:
Reverse bits + compare = palindrome check [OK]
- Using bitwise NOT instead of reversing bits
- Shifting bits incorrectly causing wrong reverse
- Comparing inverted bits instead of reversed bits
Ready to prove it?
Pass the test and this pattern moves unknown
