0
0
DSA Pythonprogramming~10 mins

Set Clear Toggle a Specific Bit in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A2
B0
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 3 instead of 2 for the bit position.
2fill in blank
medium

Complete 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'
A3
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for the bit position.
3fill in blank
hard

Fix 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'
A3
B4
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 4 instead of 3 for the bit position.
4fill in blank
hard

Fill 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'
A1
B2
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using different bit positions for set and clear.
5fill in blank
hard

Fill 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'
A0
B2
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up bit indices for toggle, set, and clear.