0
0
Embedded Cprogramming~5 mins

Clearing a specific bit in a register in Embedded C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to clear a specific bit in a register?
Clearing a bit means setting that bit to 0 while leaving other bits unchanged.
Click to reveal answer
beginner
Which bitwise operator is used to clear a specific bit in a register?
The bitwise AND operator (&) is used with the complement of a mask to clear a specific bit.
Click to reveal answer
intermediate
Given a register variable 'reg' and a bit position 'pos', write the expression to clear that bit.
reg = reg & ~(1U << pos); // This clears the bit at position 'pos' in 'reg'.
Click to reveal answer
intermediate
Why do we use the complement operator (~) when clearing a bit?
The complement operator creates a mask with all bits set to 1 except the bit to clear, which is 0. ANDing with this mask clears only that bit.
Click to reveal answer
beginner
What happens if you clear a bit that is already 0?
Nothing changes; the bit remains 0 and other bits are unaffected.
Click to reveal answer
Which operator is used to clear a specific bit in a register?
ABitwise AND (&)
BBitwise OR (|)
CBitwise XOR (^)
DBitwise NOT (~)
What does the expression (1 << pos) do?
ACreates a mask with bit 'pos' set to 1
BClears bit 'pos'
CShifts all bits right by 'pos'
DSets all bits to 1
What is the purpose of using ~ (bitwise NOT) in clearing a bit?
ATo shift bits left
BTo set all bits to 1
CTo invert the mask so the target bit is 0 and others are 1
DTo toggle the bit
If reg = 0b1111 and you clear bit 2, what is the new value of reg?
A0b1110
B0b0111
C0b1101
D0b1011
What happens if you clear a bit that is already 0?
AThe bit becomes 1
BThe bit stays 0, no change
CAll bits are cleared
DThe register resets
Explain how to clear a specific bit in a register using bitwise operations.
Think about how to keep all bits except the one you want to clear.
You got /4 concepts.
    Write a C code snippet to clear bit 5 of a variable named 'status'.
    Use shift and complement operators with AND.
    You got /1 concepts.