Reverse Bits of a Number in DSA Python - 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.
def reverse_bits(n: int) -> int:
result = 0
for i in range(32):
bit = (n >> i) & 1
result |= bit << (31 - i)
return result
This code reverses the bits of a 32-bit integer by checking each bit and placing it in the reversed position.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A loop running 32 times to process each bit.
- How many times: Exactly 32 times, once for each bit in the integer.
Since the loop runs a fixed 32 times regardless of the input number, the operations stay the same.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (small number) | 32 steps |
| 100 (larger number) | 32 steps |
| 1000 (even larger) | 32 steps |
Pattern observation: The number of steps does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the time to reverse bits is constant and does not increase as the input number grows.
[X] Wrong: "The time to reverse bits depends on the value of the number or its size."
[OK] Correct: The code always processes a fixed number of bits (32), so time does not change with the number's value.
Understanding fixed-time operations like bit manipulation shows you can reason about low-level efficiency, a valuable skill in many coding challenges.
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 bits as binary digits
Bits are the 0s and 1s that make up a number in binary form.Step 2: Define reversing bits
Reversing bits means reading the bits from right to left instead of left to right, flipping their order.Final Answer:
Flipping the order of all bits from left to right -> Option CQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with flipping bits (0 to 1)
- Thinking reversing bits means adding or multiplying
- Assuming reversing bits changes the number's value directly
n?Solution
Step 1: Analyze bit extraction and placement
reversed_num = 0 for i in range(8): reversed_num |= ((n >> i) & 1) << (7 - i) extracts each bit from right to left and places it reversed from left to right.Step 2: Check other options for correctness
reversed_num = 0 for i in range(8): reversed_num |= ((n << i) & 1) << i shifts bits incorrectly; C and D just shift without reversing bits.Final Answer:
Option A code correctly reverses bits -> Option AQuick Check:
Shift right, mask, shift left reversed position [OK]
- Using left shift instead of right shift to extract bits
- Not shifting bits to reversed positions
- Confusing bitwise OR with addition
def reverse_bits(n):
result = 0
for i in range(4):
result |= ((n >> i) & 1) << (3 - i)
return result
print(reverse_bits(13)) # 13 in binary is 1101Solution
Step 1: Convert 13 to 4-bit binary and reverse bits
13 in 4 bits is 1101. Reversing bits: 1011.Step 2: Convert reversed bits back to decimal
1011 in binary is 8+0+2+1 = 11.Final Answer:
11 -> Option DQuick Check:
1101 reversed = 1011 = 11 [OK]
- Not reversing bits correctly (reading left to right)
- Confusing binary to decimal conversion
- Using wrong bit length for reversal
n:def reverse_bits(n):
result = 0
for i in range(3):
bit = (n >> i) & 1
result |= bit << i
return resultSolution
Step 1: Check bit shifting logic
The code shifts extracted bits back to the same position i, not reversed (2 - i).Step 2: Identify correct reversed position
For 3 bits, reversed position should be (2 - i) to flip bits order.Final Answer:
It shifts bits to the same position instead of reversed position -> Option BQuick Check:
Shift bits to reversed index, not original [OK]
- Shifting bits back to original position
- Forgetting to reverse bit positions
- Incorrect loop range for bit length
n and then add it to the original number. Which Python code correctly does this?Solution
Step 1: Verify bit reversal logic for 5 bits
def reverse_bits(n): result = 0 for i in range(5): result |= ((n >> i) & 1) << (4 - i) return result print(n + reverse_bits(n)) correctly reverses bits by shifting each bit from position i to (4 - i).Step 2: Check addition of reversed bits to original
def reverse_bits(n): result = 0 for i in range(5): result |= ((n >> i) & 1) << (4 - i) return result print(n + reverse_bits(n)) prints sum of n and reversed bits, matching the problem requirement.Final Answer:
Option A code correctly reverses bits and adds to original -> Option AQuick Check:
Reverse bits with correct shifts, then add [OK]
- Using left shift instead of right shift for extraction
- Subtracting instead of adding reversed bits
- Incorrect bit length or shift positions
Ready to prove it?
Pass the test and this pattern moves unknown
