Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the 3rd bit of number n.
DSA Python
n = 8 n = n | (1 << [1]) print(bin(n))
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.
✗ Incorrect
Bits are zero-indexed from the right. The 3rd bit means index 2, so shifting 1 by 2 sets the 3rd bit.
2fill in blank
mediumComplete the code to clear the 1st bit of number n.
DSA Python
n = 15 n = n & ~(1 << [1]) print(bin(n))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the bit position.
✗ Incorrect
Clearing the 1st bit means clearing bit at index 0, so shift 1 by 0.
3fill in blank
hardFix the error in the code to toggle the 4th bit of number n.
DSA Python
n = 10 n = n ^ (1 << [1]) print(bin(n))
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.
✗ Incorrect
Toggling the 4th bit means toggling bit at index 3, so shift 1 by 3.
4fill in blank
hardFill both blanks to set and then clear the 2nd bit of number n.
DSA Python
n = 5 n = n | (1 << [1]) n = n & ~(1 << [2]) print(bin(n))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different bit positions for set and clear.
✗ Incorrect
Setting and clearing the 2nd bit means bit index 1 for both operations.
5fill in blank
hardFill all three blanks to toggle the 1st bit, set the 3rd bit, and clear the 2nd bit of number n.
DSA Python
n = 0 n = n ^ (1 << [1]) n = n | (1 << [2]) n = n & ~(1 << [3]) print(bin(n))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up bit indices for toggle, set, and clear.
✗ Incorrect
Toggling 1st bit is index 0, setting 3rd bit is index 2, clearing 2nd bit is index 1.