Reverse Bits of a Number in DSA C - Time & Space Complexity
We want to understand how the time needed to reverse bits in a number changes as the number size changes.
Specifically, how does the number of steps grow when we reverse bits of a number?
Analyze the time complexity of the following code snippet.
unsigned int reverseBits(unsigned int n) {
unsigned 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 unsigned integer by shifting and masking each bit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs 32 times, processing one bit each time.
- How many times: Exactly 32 times, once for each bit in the number.
Since the loop runs a fixed 32 times regardless of the number's value, the steps do not grow with input size.
| Input Size (bits) | Approx. Operations |
|---|---|
| 10 | 32 (fixed) |
| 100 | 32 (fixed) |
| 1000 | 32 (fixed) |
Pattern observation: The number of operations stays the same because the bit size is fixed.
Time Complexity: O(1)
This means the time to reverse bits does not grow with input size because the number of bits is fixed.
[X] Wrong: "The time to reverse bits grows with the number's value or size."
[OK] Correct: The code always processes a fixed number of bits (32), so time depends on bit length, not the number's value.
Understanding fixed-time operations like bit reversal helps you reason about low-level data manipulation efficiently, a useful skill in many coding challenges.
"What if we changed the input to a variable bit-length 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, second leftmost becomes second rightmost, and so on.Final Answer:
Flipping the order of bits from left to right -> Option CQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with bitwise NOT
- Thinking reversing bits adds or subtracts from number
- Mixing bit shifting with reversing bits
n?Solution
Step 1: Analyze the correct loop logic
The loop shiftsrevleft by 1, ORs with(n & 1), then shiftsnright by 1. This reverses bits correctly.Step 2: Check distractor options
rev |= (n & 1) << i; n >>= 1;copies bits in same order.return ~n;is bitwise NOT, not reverse.return n << 1;shifts bits left, not reverse.Final Answer:
rev <<= 1; rev |= (n & 1); n >>= 1; -> Option DQuick Check:
Shift rev left + add bit from n = reverse bits [OK]
- Using bitwise NOT (~) instead of reversing bits
- Not shifting rev before adding new bit
- Shifting bits incorrectly causing wrong order
unsigned char reverseBits(unsigned char n) {
unsigned char rev = 0;
for(int i = 0; i < 8; i++) {
rev <<= 1;
rev |= (n & 1);
n >>= 1;
}
return rev;
}
int main() {
unsigned char x = 13; // binary 00001101
unsigned char y = reverseBits(x);
printf("%u", y);
return 0;
}Solution
Step 1: Convert 13 to binary and reverse bits
13 in 8-bit binary is 00001101. Reversing bits gives 10110000.Step 2: Convert reversed binary to decimal
10110000 binary = 128 + 32 + 16 = 176 decimal.Final Answer:
176 -> Option BQuick Check:
Reverse 00001101 = 10110000 = 176 [OK]
- Confusing reversed bits with original number
- Calculating decimal incorrectly from binary
- Ignoring leading zeros in binary
unsigned char reverseBits4(unsigned char n) {
unsigned char rev = 0;
for(int i = 0; i < 4; i++) {
rev |= (n & 1) << i;
n >>= 1;
}
return rev;
}Solution
Step 1: Analyze bit addition logic
The code adds bits to rev using rev |= (n & 1) << i, which places bits in original order, not reversed.Step 2: Understand correct reversing method
To reverse bits, rev should be shifted left before adding new bit, not ORed at position i.Final Answer:
It does not shift rev left before adding bits, so bits are not reversed -> Option AQuick Check:
Shift rev left before adding bit to reverse [OK]
- Adding bits at same positions without shifting rev
- Confusing left and right shifts on n
- Looping wrong number of times
Solution
Step 1: Compute reversed bits
Reverse bits using a loop to obtain the reversed numberrev.Step 2: Check if reversed is palindrome
The reversed number is a palindrome if it reads the same forwards and backwards, i.e.,rev == original(sincereverse(rev) == originalalways holds for a correct reverse).Final Answer:
Reverse bits using a loop, then compare reversed number with original -> Option AQuick Check:
rev == original means rev is palindrome [OK]
- Thinking double reverse checks palindrome (it only verifies the reverse function)
- Trying to check palindrome without computing reversed bits
- Confusing bit reversal with simple shifts
Ready to prove it?
Pass the test and this pattern moves unknown
