Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result variable to zero.
DSA C#
int result = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result to 1 or n causes wrong bit reversal.
Using -1 leads to incorrect bit patterns.
✗ Incorrect
We start with result = 0 because we will build the reversed bits from scratch.
2fill in blank
mediumComplete the code to extract the least significant bit from n.
DSA C#
int bit = n [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using | or ^ does not isolate the last bit correctly.
Using >> shifts bits instead of extracting.
✗ Incorrect
Using bitwise AND with 1 extracts the last bit of n.
3fill in blank
hardFix the error in the code to shift result left by one bit.
DSA C#
result = result [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - changes the value incorrectly.
Using >> shifts bits in the wrong direction.
✗ Incorrect
Left shift (<<) moves bits to the left, making space for the next bit.
4fill in blank
hardFill both blanks to update result with the extracted bit and shift n right.
DSA C#
result = result | [1]; n = n [2] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using result instead of bit causes wrong bit addition.
Shifting n left instead of right processes bits incorrectly.
✗ Incorrect
We add the extracted bit to result using OR, then shift n right to process next bit.
5fill in blank
hardFill all three blanks to complete the loop that reverses bits of n.
DSA C#
for (int i = 0; i < [1]; i++) { int bit = n [2] 1; result = (result [3] 1) | bit; n = n >> 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping less than 32 times misses bits.
Using | instead of & to extract bit causes errors.
Shifting result right instead of left reverses bits incorrectly.
✗ Incorrect
We loop 32 times for 32 bits, extract bit with &, shift result left with <<, then add bit.
