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)?
✗ Incorrect
Setting bit 2 means turning the third bit from right to 1. 8 is 1000, setting bit 2 adds 4 (0100), so 1000 | 0100 = 1100 (12).
Which operation toggles a bit at position pos?
✗ Incorrect
XOR (^) with (1 << pos) flips the bit at pos: 0 becomes 1, 1 becomes 0.
Clearing a bit means:
✗ Incorrect
Clearing a bit means making that bit 0.
What does the expression
num & ~(1 << pos) do?✗ Incorrect
It clears the bit at position pos by ANDing with the inverse mask.
If num = 5 (binary 0101), what is the result of toggling bit 0?
✗ Incorrect
Toggling bit 0 flips the rightmost bit: 0101 ^ 0001 = 0100 (4).
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.