Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result variable to zero.
DSA Python
def reverse_bits(n): result = [1] for _ in range(32): result = (result << 1) | (n & 1) n >>= 1 return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with 1 or n causes wrong output.
✗ Incorrect
We start with result = 0 because we build the reversed bits from scratch.
2fill in blank
mediumComplete the code to extract the least significant bit of n.
DSA Python
def reverse_bits(n): result = 0 for _ in range(32): bit = n [1] 1 result = (result << 1) | bit n >>= 1 return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR or XOR instead of AND causes wrong bit extraction.
✗ Incorrect
Use bitwise AND & with 1 to get the last bit of n.
3fill in blank
hardFix the error in the code to shift n right by one bit.
DSA Python
def reverse_bits(n): result = 0 for _ in range(32): bit = n & 1 result = (result << 1) | bit n = n [1] 1 return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using left shift instead of right shift causes infinite loop or wrong bits.
✗ Incorrect
Shift n right by 1 bit using >> to move to the next bit.
4fill in blank
hardFill both blanks to complete the loop that reverses bits.
DSA Python
def reverse_bits(n): result = 0 for [1] in range([2]): bit = n & 1 result = (result << 1) | bit n >>= 1 return result
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable
n in loop causes confusion.Wrong loop count causes incomplete reversal.
✗ Incorrect
Use a throwaway variable _ for the loop and iterate 32 times for 32 bits.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps numbers to their reversed bits.
DSA Python
reversed_bits = [1]: reverse_bits([2]) for [3] in range(5)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.
Using
n instead of x breaks comprehension.✗ Incorrect
Use x as the variable and key, and call reverse_bits(x) for values.