Reverse Bits of a Number in DSA Javascript - 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 bigger numbers?
Analyze the time complexity of the following code snippet.
function reverseBits(n) {
let result = 0;
for (let i = 0; i < 32; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
This code reverses the bits of a 32-bit number by shifting and masking each bit.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A for-loop running 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, the steps do not grow with the number's value.
| 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 stays the same regardless of input size because the bit count 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: "Reversing bits takes longer for bigger numbers because they have more digits."
[OK] Correct: The number of bits is fixed (32 bits here), so the steps are always the same no matter the number's size.
Understanding fixed-time operations like bit manipulation shows you can reason about low-level efficiency, a useful skill in many coding challenges.
"What if we changed the input to 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.Final Answer:
Flipping the order of all bits from left to right -> Option AQuick Check:
Reverse bits = flip order [OK]
- Confusing reversing bits with flipping bits (0 to 1)
- Thinking reversing bits changes the number by adding or multiplying
- Assuming reversing bits means reversing digits in decimal
n?Solution
Step 1: Analyze the loop and bit operations
let result = 0; for(let i=0; i<8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } shifts result left, adds least significant bit of n, then shifts n right. This reverses bits correctly.Step 2: Check other options for errors
let result = 0; for(let i=0; i<8; i++) { result |= (n & 1) << i; n >>= 1; } does not reverse bits, it copies bits in same order. let result = 0; for(let i=7; i>0; i--) { result |= (n & 1) << i; n >>= 1; } shifts bits incorrectly. let result = 0; for(let i=0; i<8; i++) { result += (n & 1) << i; n <<= 1; } shifts n left, which is wrong.Final Answer:
let result = 0; for(let i=0; i<8; i++) { result <<= 1; result |= (n & 1); n >>= 1; } -> Option CQuick Check:
Shift result left + add bit from n right [OK]
- Shifting n left instead of right
- Not shifting result before adding bit
- Using wrong loop direction or bit positions
function reverseBits(n) {
let result = 0;
for(let i = 0; i < 4; i++) {
result <<= 1;
result |= (n & 1);
n >>= 1;
}
return result;
}
console.log(reverseBits(13));Solution
Step 1: Convert 13 to 4-bit binary
13 in binary (4 bits) is 1101.Step 2: Reverse bits step-by-step
Loop 4 times: result builds as follows:
i=0: result=0<<1=0 + (13&1=1) =1
n=13>>1=6 (0110)
i=1: result=1<<1=2 + (6&1=0) =2
n=6>>1=3 (0011)
i=2: result=2<<1=4 + (3&1=1) =5
n=3>>1=1 (0001)
i=3: result=5<<1=10 + (1&1=1) =11Final Answer:
11 -> Option BQuick Check:
Reverse 1101 = 1011 (binary 11 decimal) [OK]
- Confusing decimal and binary values
- Not shifting result before OR
- Looping wrong number of times
function reverseBits(n) {
let result = 0;
for(let i = 0; i < 8; i++) {
result |= (n & 1);
result <<= 1;
n >>= 1;
}
return result;
}Solution
Step 1: Identify order of operations
Current code ORs bit then shifts result left, causing last bit to be lost.Step 2: Correct by shifting result left before OR
Shift result left first, then OR with current bit to build reversed bits correctly.Final Answer:
Swap the order: shift result left before OR operation -> Option DQuick Check:
Shift before OR to keep all bits [OK]
- Shifting result after OR loses last bit
- Shifting n left instead of right
- Wrong loop count for bit size
Solution
Step 1: Reverse bits of the number
Use a loop to reverse all 16 bits correctly.Step 2: Check palindrome by comparing reversed number with its own reverse
If reversed number equals its reverse, it is a binary palindrome.Final Answer:
Reverse bits using a loop, then check if reversed number equals its own reverse -> Option AQuick Check:
Palindrome check = reversed == reverse(reversed) [OK]
- Comparing reversed number directly to original (wrong for palindrome)
- Checking decimal evenness instead of binary palindrome
- Reversing bits twice unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
