0
0
DSA Pythonprogramming

Reverse Bits of a Number in DSA Python

Choose your learning style9 modes available
Mental Model
Flip the order of bits in a number so the last bit becomes first and the first becomes last.
Analogy: Imagine a row of light switches turned on or off. Reversing bits is like walking from the end of the row back to the start, flipping the order of the switches exactly.
Original bits: 0 0 0 0 1 1 0 1
Reversed bits: 1 0 1 1 0 0 0 0
Dry Run Walkthrough
Input: number = 13 (binary 00001101), reverse all 8 bits
Goal: Get the number whose bits are reversed: 10110000 (binary) = 176 decimal
Step 1: Extract least significant bit (LSB) 1 and add to result
result=1 (binary 00000001), number shifted right to 00000110
Why: Start building reversed number from rightmost bit
Step 2: Shift result left and add next LSB 0
result=2 (binary 00000010), number shifted right to 00000011
Why: Make room for next bit and add it
Step 3: Shift result left and add next LSB 1
result=5 (binary 00000101), number shifted right to 00000001
Why: Add next bit in reversed order
Step 4: Shift result left and add next LSB 1
result=11 (binary 00001011), number shifted right to 00000000
Why: Add next bit; number now zero
Step 5: Shift result left and add 0 (since number is zero, bits are zero)
result=22 (binary 00010110)
Why: Fill remaining bits with zeros
Step 6: Shift result left and add 0
result=44 (binary 00101100)
Why: Continue filling zeros
Step 7: Shift result left and add 0
result=88 (binary 01011000)
Why: Continue filling zeros
Step 8: Shift result left and add 0
result=176 (binary 10110000)
Why: Final reversed bits complete
Result:
Reversed bits: 10110000 (binary) = 176 decimal
Annotated Code
DSA Python
class Solution:
    def reverseBits(self, n: int) -> int:
        result = 0
        for _ in range(8):  # fixed 8 bits for this example
            result <<= 1  # shift result left to make room for next bit
            result |= n & 1  # add least significant bit of n to result
            n >>= 1  # shift n right to process next bit
        return result

# Driver code
sol = Solution()
input_number = 13
output = sol.reverseBits(input_number)
print(output)
result <<= 1 # shift result left to make room for next bit
shift reversed bits left to add new bit
result |= n & 1 # add least significant bit of n to result
extract current bit from n and add to result
n >>= 1 # shift n right to process next bit
move to next bit in original number
OutputSuccess
176
Complexity Analysis
Time: O(k) because we process each of the k bits once (here k=8)
Space: O(1) because only a few variables are used regardless of input size
vs Alternative: Naive approach might convert to string and reverse, which uses extra space and is slower; bitwise approach is efficient and constant space
Edge Cases
number = 0 (all bits zero)
result is 0 after reversal
DSA Python
for _ in range(8):
number = 255 (all bits one for 8 bits)
result is 255 after reversal (same bits)
DSA Python
for _ in range(8):
number with leading zeros like 1 (00000001)
result reverses bits correctly to 128 (10000000)
DSA Python
for _ in range(8):
When to Use This Pattern
When you need to reverse the order of bits in a number, use bitwise shifting and masking to build the reversed number efficiently.
Common Mistakes
Mistake: Not shifting the result left before adding the new bit, causing bits to overwrite
Fix: Always shift result left before OR-ing the next bit
Mistake: Looping fewer or more times than the bit length, causing incorrect reversal
Fix: Loop exactly the number of bits you want to reverse (e.g., 8 or 32)
Summary
Reverses the bits of a number by shifting and masking each bit.
Use when you need the mirror image of bits in a fixed-size number.
Shift the result left before adding each bit from the original number to build reversed bits.