Reverse Bits of a Number in DSA Go - 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 one by one?
Analyze the time complexity of the following code snippet.
func reverseBits(num uint32) uint32 {
var result uint32 = 0
for i := 0; i < 32; i++ {
result <<= 1
result |= num & 1
num >>= 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, once for each bit.
- How many times: Exactly 32 times, fixed for 32-bit numbers.
Since the loop runs a fixed 32 times regardless of the number's value, the operations stay the same.
| Input Size (bits) | Approx. Operations |
|---|---|
| 10 | 32 (fixed) |
| 100 | 32 (fixed) |
| 1000 | 32 (fixed) |
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 stays constant no matter what number you use, because the bit count is fixed.
[X] Wrong: "The time grows with the value of the number because bigger numbers have more bits to reverse."
[OK] Correct: The number of bits is fixed (32 bits here), so the loop always runs the same number of times regardless of the number's size.
Understanding fixed-size operations like bit reversal helps you explain why some algorithms run in constant time, which is a valuable skill 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 representation
Numbers are stored as bits (0s and 1s) in a sequence from left (most significant) to right (least significant).Step 2: Define reversing bits
Reversing bits means taking the bit sequence and flipping it so the first bit becomes last, second becomes second last, and so on.Final Answer:
Flipping the order of bits from left to right -> Option BQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with bitwise NOT (flip 0s and 1s)
- Thinking reversing bits means adding or subtracting
- Mixing reversing bits with shifting bits
n?Solution
Step 1: Analyze bit reversal logic
We shift rev left to make space, add the least significant bit of n, then shift n right to process next bit.Step 2: Check code correctness
var rev uint8 = 0 for i := 0; i < 8; i++ { rev <<= 1 rev |= n & 1 n >>= 1 } correctly shifts rev left, ORs with n's last bit, then shifts n right. Others either shift wrong direction or add bits incorrectly.Final Answer:
The code in A correctly reverses bits -> Option DQuick Check:
Shift rev left + add n's LSB + shift n right = correct [OK]
- Shifting rev right instead of left
- Shifting n left instead of right
- Adding bits without shifting rev
func reverseBits(n uint8) uint8 {
var rev uint8 = 0
for i := 0; i < 8; i++ {
rev <<= 1
rev |= n & 1
n >>= 1
}
return rev
}
fmt.Println(reverseBits(13))Note: 13 in binary is 00001101.
Solution
Step 1: Convert 13 to binary and reverse bits
13 in 8 bits: 00001101. Reversing bits: 10110000.Step 2: Convert reversed bits back to decimal
10110000 binary = 128 + 32 + 16 = 176 decimal.Final Answer:
176 -> Option AQuick Check:
Reverse(00001101) = 10110000 = 176 [OK]
- Confusing bit order and reversing incorrectly
- Calculating decimal from wrong binary
- Ignoring leading zeros in 8-bit representation
func reverse4Bits(n uint8) uint8 {
var rev uint8 = 0
for i := 0; i < 4; i++ {
rev |= (n & 1) << i
n >>= 1
}
return rev
}Solution
Step 1: Understand the code logic
The code shifts bits of n right, then sets rev bits by shifting left by i, which copies bits in same order.Step 2: Identify why bits are not reversed
Reversing bits requires shifting rev left each time and adding n's last bit, not shifting by i.Final Answer:
The bits are not reversed; they are copied in the same order -> Option CQuick Check:
Shifting rev by i copies bits, not reverses [OK]
- Confusing bit copying with reversing
- Wrong loop count for bit size
- Incorrect variable types for bits
Solution
Step 1: Reverse bits of the number
Use a loop to reverse all 16 bits to get reversed number.Step 2: Check if reversed number is a palindrome
To verify if reversed reads the same forwards and backwards, compute reverse(reversed) and check if it equals reversed. Since reverse(reversed) = original, this is equivalent to comparing reversed with original.Final Answer:
Reverse bits using a loop, then compare reversed number with original -> Option AQuick Check:
reversed == original iff reversed is palindrome [OK]
- Double reversing and comparing to original (always true, doesn't check palindrome)
- Trying to check equality without computing the reverse
- Using shifting without proper bit reversal
Ready to prove it?
Pass the test and this pattern moves unknown
