0
0
DSA Pythonprogramming~5 mins

Set Clear Toggle a Specific Bit in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to set a specific bit in a number?
Setting a bit means changing that bit to 1, regardless of its current value.
Click to reveal answer
beginner
How do you clear a specific bit in a number?
Clearing a bit means changing that bit to 0, regardless of its current value.
Click to reveal answer
beginner
What does toggle a bit mean?
Toggling a bit means changing it from 0 to 1 or from 1 to 0.
Click to reveal answer
beginner
Which operator is used to set a bit at position pos in Python?
Use the bitwise OR operator (|) with (1 << pos). Example: num | (1 << pos)
Click to reveal answer
beginner
How do you clear a bit at position pos using bitwise operators?
Use bitwise AND (&) with the negation of (1 << pos): num & ~(1 << pos)
Click to reveal answer
What is the result of setting bit 2 (0-based) in the number 8 (binary 1000)?
A12 (binary 1100)
B10 (binary 1010)
C14 (binary 1110)
D8 (binary 1000)
Which operation toggles a bit at position pos?
Anum | (1 << pos)
Bnum & ~(1 << pos)
Cnum << pos
Dnum ^ (1 << pos)
Clearing a bit means:
AFlipping its value
BSetting it to 1
CSetting it to 0
DLeaving it unchanged
What does the expression num & ~(1 << pos) do?
ASets bit pos to 1
BClears bit pos to 0
CToggles bit pos
DChecks if bit pos is set
If num = 5 (binary 0101), what is the result of toggling bit 0?
A4 (binary 0100)
B7 (binary 0111)
C1 (binary 0001)
D5 (binary 0101)
Explain how to set, clear, and toggle a specific bit in a number using bitwise operators.
Think about how each operator affects bits at a given position.
You got /3 concepts.
    Describe a real-life example where setting, clearing, or toggling bits might be useful.
    Think about simple yes/no settings stored in one number.
    You got /3 concepts.