Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the bit at position 3 in the integer 'num'.
DSA Java
int result = num [1] (1 << 3);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' which clears bits instead of setting.
Using '^' which toggles bits instead of setting.
Using '<<' which is a shift operator, not a bitwise operator.
✗ Incorrect
Using the bitwise OR operator '|' sets the bit at position 3 without changing other bits.
2fill in blank
mediumComplete the code to clear the bit at position 2 in the integer 'num'.
DSA Java
int result = num [1] ~(1 << 2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' which sets bits instead of clearing.
Using '^' which toggles bits instead of clearing.
Using '~' alone which inverts all bits.
✗ Incorrect
Using '&' with the negated mask clears the bit at position 2 while keeping others unchanged.
3fill in blank
hardFix the error in the code to toggle the bit at position 1 in 'num'.
DSA Java
int result = num [1] (1 << 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' which clears bits instead of toggling.
Using '|' which sets bits instead of toggling.
Using '~' which inverts all bits.
✗ Incorrect
The XOR '^' operator toggles the bit at position 1: if it was 0, it becomes 1; if 1, it becomes 0.
4fill in blank
hardFill both blanks to set the bit at position 'pos' in 'num'.
DSA Java
int result = num [1] (1 [2] pos);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' instead of '|' to set bits.
Using '>>' which shifts bits right, not left.
Mixing up the order of operators.
✗ Incorrect
Shift 1 left by 'pos' bits using '<<' to create the mask, then OR '|' with 'num' to set that bit.
5fill in blank
hardFill all three blanks to clear the bit at position 'pos' in 'num'.
DSA Java
int result = num [1] ~ (1 [2] [3]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' instead of '&' to clear bits.
Using '>>' instead of '<<' for shifting.
Forgetting to negate the mask.
✗ Incorrect
Shift 1 left by 'pos' bits using '<<', then negate '~' to create mask, and AND '&' with 'num' to clear the bit.
