Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the result variable to zero.
DSA C
unsigned int reverseBits(unsigned int n) {
unsigned int result = [1];
for (int i = 0; i < 32; i++) {
// loop body
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing result with n instead of 0.
Using negative values for unsigned int.
✗ 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 of n.
DSA C
unsigned int reverseBits(unsigned int n) {
unsigned int result = 0;
for (int i = 0; i < 32; i++) {
unsigned int bit = n [1] 1;
n >>= 1;
// use bit
}
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 to extract bits.
Using shift operators incorrectly here.
✗ Incorrect
Using bitwise AND with 1 extracts the least significant bit of n.
3fill in blank
hardFix the error in the code to shift the result left before adding the extracted bit.
DSA C
unsigned int reverseBits(unsigned int n) {
unsigned int result = 0;
for (int i = 0; i < 32; i++) {
unsigned int bit = n & 1;
n >>= 1;
result = (result [1] 1) | bit;
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift instead of left shift.
Using addition instead of bitwise OR.
✗ Incorrect
We shift result left by 1 to make space for the new bit on the right.
4fill in blank
hardFill both blanks to complete the loop that reverses bits of n.
DSA C
unsigned int reverseBits(unsigned int n) {
unsigned int result = 0;
for (int i = 0; i [1] 0; i [2] 32) {
unsigned int bit = n & 1;
n >>= 1;
result = (result << 1) | bit;
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or <= incorrectly in the loop condition.
Starting i at 1 instead of 0.
✗ Incorrect
The loop starts with i = 0 and runs while i < 32 to process all bits.
5fill in blank
hardFill all three blanks to create a function that reverses bits of an unsigned int.
DSA C
unsigned int reverseBits(unsigned int n) {
unsigned int result = [1];
for (int i = 0; i [2] 0; i [3] 32) {
unsigned int bit = n & 1;
n >>= 1;
result = (result << 1) | bit;
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to initialize result to zero.
Using wrong loop conditions or increments.
✗ Incorrect
Initialize result to 0, start i at 0, loop while i < 32, and increment i by 1 each time.
