Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to shift bits to the right by one position.
DSA Java
int shifted = n [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using left shift '<<' instead of right shift '>>'.
Using bitwise AND '&' instead of shift.
✗ Incorrect
The right shift operator '>>' moves bits to the right by the specified number of positions.
2fill in blank
mediumComplete the code to extract the least significant bit of n.
DSA Java
int bit = n [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR '|' instead of AND '&'.
Using XOR '^' which toggles bits.
✗ Incorrect
The bitwise AND '&' with 1 extracts the least significant bit of n.
3fill in blank
hardFix the error in the code to add the extracted bit to the reversed number.
DSA Java
reversed = (reversed [1] 1) [2] bit;
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift '>>' instead of left shift '<<'.
Using AND '&' instead of OR '|'.
✗ Incorrect
Shift reversed left by 1 to make space, then OR '|' with bit to add it.
4fill in blank
hardFill both blanks to complete the loop that reverses bits of n.
DSA Java
for (int i = 0; i < [1]; i++) { int bit = n [2] 1; n = n >>> 1; reversed = (reversed << 1) | bit; }
Attempts:
3 left
💡 Hint
Common Mistakes
Looping 16 times instead of 32.
Using '|' instead of '&' to extract bit.
✗ Incorrect
We loop 32 times for 32 bits, and use '&' to extract the least significant bit.
5fill in blank
hardFill all three blanks to complete the reverseBits method.
DSA Java
public int reverseBits(int n) {
int reversed = 0;
for (int i = 0; i < [1]; i++) {
int bit = n [2] 1;
n = n >>> 1;
reversed = (reversed [3] 1) | bit;
}
return reversed;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>>' instead of '<<' to shift reversed.
Looping fewer than 32 times.
Using '|' instead of '&' to extract bit.
✗ Incorrect
Loop 32 times, extract bit with '&', shift reversed left with '<<' before adding bit.
