0
0
Embedded Cprogramming~5 mins

Setting 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 set a specific bit in a register?
Setting a specific bit means changing that bit's value to 1 while leaving other bits unchanged.
Click to reveal answer
beginner
Which operator is commonly used in C to set a specific bit in a register?
The bitwise OR operator (|) is used to set a specific bit.
Click to reveal answer
intermediate
Explain the expression: register |= (1 << n);
It shifts 1 to the left by n positions to create a mask with only bit n set, then ORs it with the register to set bit n.
Click to reveal answer
intermediate
Why is it important to use bitwise OR instead of assignment when setting a bit?
Using OR preserves other bits in the register; assignment would overwrite all bits, losing existing data.
Click to reveal answer
beginner
Write a C code snippet to set bit 3 of a register named REG.
REG |= (1 << 3);
Click to reveal answer
What does the expression (1 << 5) do in C?
ASets bit 5 to 1
BShifts the number 1 left by 5 bits
CClears bit 5
DPerforms bitwise AND with 5
Which operator is used to set a bit without changing other bits?
A| (OR)
B^ (XOR)
C& (AND)
D~ (NOT)
What happens if you use assignment (=) instead of OR (|=) to set a bit?
AOnly the targeted bit is set
BAll bits are cleared
CThe entire register is overwritten, losing other bits
DNothing changes
How do you set bit 0 of a register named REG?
AREG &= ~(1 << 0);
BREG |= (1 >> 0);
CREG ^= (1 << 0);
DREG |= (1 << 0);
What is the result of REG |= (1 << n); if bit n was already set?
ABit n remains set
BRegister value becomes zero
CAll bits are cleared
DBit n is cleared
Describe how to set a specific bit in a register using C code.
Think about creating a mask with 1 shifted to the bit position.
You got /3 concepts.
    Explain why using |= is better than = when setting a bit in a register.
    Consider what happens to bits not targeted.
    You got /3 concepts.