Recall & Review
beginner
What does XOR operation do when used to toggle a bit?
XOR flips the bit: if the bit is 0, it becomes 1; if it is 1, it becomes 0.
Click to reveal answer
beginner
How do you toggle the 3rd bit of a variable 'x' using XOR in C?
Use the expression: x = x ^ (1 << 2); because bits start at 0 from the right.
Click to reveal answer
intermediate
Why is XOR preferred for toggling bits instead of using OR or AND?
Because XOR flips bits without affecting others, while OR sets bits and AND clears bits.
Click to reveal answer
intermediate
What happens if you XOR the same bit twice?
The bit returns to its original value because XORing twice cancels the toggle.
Click to reveal answer
beginner
Write a C code snippet to toggle bits 0 and 1 of a byte variable 'flags'.
flags = flags ^ 0x03; // 0x03 is binary 00000011, toggles bits 0 and 1
Click to reveal answer
What is the result of toggling bit 1 of 0b00000100 using XOR?
✗ Incorrect
Toggling bit 1 (second bit from right) flips 0 to 1, so 0b00000100 becomes 0b00000110.
Which operator is used to toggle bits in embedded C?
✗ Incorrect
XOR (^) flips bits where the mask has 1s, perfect for toggling.
If you XOR a bit with 0, what happens to the bit?
✗ Incorrect
XOR with 0 leaves the bit unchanged.
What does the expression (1 << 4) represent?
✗ Incorrect
(1 << 4) shifts 1 left by 4 bits, setting bit 4.
Toggling the same bit twice results in:
✗ Incorrect
XOR toggling twice returns the bit to its original value.
Explain how XOR can be used to toggle a specific bit in a byte variable.
Think about how XOR changes 0 to 1 and 1 to 0.
You got /3 concepts.
Describe why XOR is better than AND or OR for toggling bits.
Consider what happens to bits not targeted by the operation.
You got /4 concepts.