Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the 3rd bit of variable x.
DSA C
x = x | (1 << [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 for the bit position.
Using bitwise AND instead of OR.
✗ Incorrect
Bits are zero-indexed from the right. To set the 3rd bit, shift 1 by 2 positions.
2fill in blank
mediumComplete the code to clear the 4th bit of variable y.
DSA C
y = y & ~(1 << [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 4 instead of 3 for the bit position.
Forgetting to invert the mask with ~.
✗ Incorrect
To clear the 4th bit, shift 1 by 3 positions and invert the mask before AND.
3fill in blank
hardFix the error in toggling the 5th bit of variable z.
DSA C
z = z ^ (1 << [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 instead of 4 for the bit position.
Using OR or AND instead of XOR for toggling.
✗ Incorrect
To toggle the 5th bit, shift 1 by 4 positions because bits start at 0.
4fill in blank
hardFill both blanks to set bit 1 and clear bit 3 of variable a.
DSA C
a = (a | (1 << [1])) & ~(1 << [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up bit positions for set and clear.
Not negating the mask when clearing.
✗ Incorrect
Set bit 1 by shifting 1 by 1, clear bit 3 by shifting 1 by 3 and negating.
5fill in blank
hardFill all three blanks to toggle bit 0, set bit 2, and clear bit 4 of variable b.
DSA C
b = (b ^ (1 << [1])) | (1 << [2]); b = b & ~(1 << [3]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong bit positions for toggle, set, or clear.
Forgetting to negate the mask when clearing.
✗ Incorrect
Toggle bit 0 by shifting 1 by 0, set bit 2 by shifting 1 by 2, clear bit 4 by shifting 1 by 4 and negating.
