Recall & Review
beginner
What does the OR operator (|) do when used to set bits in embedded C?
The OR operator sets specific bits to 1 without changing other bits. It combines bits so if either bit is 1, the result bit is 1.
Click to reveal answer
beginner
How do you set the 3rd bit of a variable 'flags' using OR in embedded C?
Use: flags = flags | (1 << 2); This shifts 1 to the 3rd bit position and ORs it with flags to set that bit.
Click to reveal answer
beginner
Why is OR used instead of AND to set bits?
AND clears bits (sets to 0) if the mask bit is 0. OR sets bits to 1 if the mask bit is 1, which is what we want to set bits.
Click to reveal answer
beginner
What happens if you OR a variable with 0?
The variable stays the same because OR with 0 leaves bits unchanged.
Click to reveal answer
beginner
Explain the expression: flags |= (1 << 5);
It sets the 6th bit of 'flags' to 1. The |= operator is shorthand for flags = flags | (1 << 5).
Click to reveal answer
What does the expression 'flags | (1 << 3)' do?
✗ Incorrect
Shifting 1 by 3 moves it to the 4th bit position (counting from 0). OR sets that bit to 1.
Which operator is used to set bits without affecting other bits?
✗ Incorrect
OR sets bits to 1 where the mask has 1s, leaving other bits unchanged.
What is the result of 'flags | 0'?
✗ Incorrect
OR with 0 leaves all bits unchanged.
How do you set the 1st bit of a variable 'x' using OR?
✗ Incorrect
OR with 1 sets the least significant bit (1st bit) to 1.
What does 'flags |= (1 << n);' do?
✗ Incorrect
It sets the nth bit of flags to 1 using OR and compound assignment.
Explain how the OR operator is used to set specific bits in a variable in embedded C.
Think about how 1 shifted left creates a mask to set one bit.
You got /4 concepts.
Describe the difference between using OR and AND operators when manipulating bits.
Consider what happens when you combine bits with 1 or 0.
You got /4 concepts.