0
0
Embedded Cprogramming~5 mins

OR for setting bits in Embedded C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ASets the 4th bit of flags to 1
BClears the 4th bit of flags
CToggles the 4th bit of flags
DChecks if the 4th bit of flags is 1
Which operator is used to set bits without affecting other bits?
AXOR (^)
BAND (&)
COR (|)
DNOT (~)
What is the result of 'flags | 0'?
Aflags unchanged
BAll bits set to 1
CAll bits cleared to 0
DError
How do you set the 1st bit of a variable 'x' using OR?
Ax = x << 1;
Bx = x & 1;
Cx = x ^ 1;
Dx = x | 1;
What does 'flags |= (1 << n);' do?
AClears the nth bit
BSets the nth bit
CToggles the nth bit
DChecks the nth bit
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.