What does setting a bit in a register mean in embedded C?
Setting a bit means changing that bit to 1 without affecting other bits in the register. This is usually done using the OR operator (|) with a mask that has 1 at the bit position to set.
Click to reveal answer
beginner
How do you clear a specific bit in a register?
To clear a bit (set it to 0), use the AND operator (&) with the inverse (~) of a mask that has 1 at the bit position you want to clear. This keeps other bits unchanged.
Click to reveal answer
intermediate
Explain toggling a bit in a register.
Toggling a bit means flipping it: if it was 0, it becomes 1; if it was 1, it becomes 0. Use the XOR operator (^) with a mask that has 1 at the bit position to toggle.
Click to reveal answer
beginner
What is a bit mask and why is it important?
A bit mask is a binary number used to select or modify specific bits in a register. It helps isolate or change bits without affecting others, making bit manipulation safe and precise.
Click to reveal answer
beginner
How do you check if a specific bit is set in a register?
Use the AND operator (&) with a mask that has 1 at the bit position. If the result is not zero, the bit is set (1); if zero, the bit is clear (0).
Click to reveal answer
Which operator is used to set a bit in a register without changing other bits?
A| (OR)
B& (AND)
C^ (XOR)
D~ (NOT)
✗ Incorrect
The OR operator (|) sets bits to 1 where the mask has 1, leaving other bits unchanged.
How do you clear bit 3 of a register named REG?
AREG = ~REG;
BREG = REG | (1 << 3);
CREG = REG ^ (1 << 3);
DREG = REG & ~(1 << 3);
✗ Incorrect
Using AND with the inverse mask clears bit 3 without affecting other bits.
What does toggling a bit mean?
ASetting it to 1
BSetting it to 0
CFlipping its value
DChecking its value
✗ Incorrect
Toggling flips the bit: 0 becomes 1, and 1 becomes 0.
Which operator helps check if a bit is set?
A& (AND)
B| (OR)
C^ (XOR)
D<< (left shift)
✗ Incorrect
AND with a mask isolates the bit to check if it is set.
Why use a bit mask in register manipulation?
ATo change all bits at once
BTo isolate or modify specific bits safely
CTo convert bits to decimal
DTo clear the entire register
✗ Incorrect
Bit masks help target specific bits without affecting others.
Describe how to set, clear, toggle, and check a bit in a register using bitwise operators.
Think about how each operator affects bits when combined with a mask.
You got /4 concepts.
Explain the role of a bit mask in register bit manipulation and why it is important.
Consider how you can change one light bulb in a string without turning off others.
You got /4 concepts.
Practice
(1/5)
1. Which bitwise operator is used to set a specific bit in a register without changing other bits? Example: Set bit 3 in register REG
easy
A. Bitwise OR (|)
B. Bitwise AND (&)
C. Bitwise XOR (^)
D. Bitwise NOT (~)
Solution
Step 1: Understand setting a bit
Setting a bit means making that bit 1 without changing others.
Step 2: Use bitwise OR
Bitwise OR with 1 at the target bit sets it to 1, others stay same.
Final Answer:
Bitwise OR (|) -> Option A
Quick Check:
Set bit = OR [OK]
Hint: Use OR (|) to set bits without changing others [OK]
Common Mistakes:
Using AND (&) to set bits (it clears bits)
Using XOR (^) which toggles bits
Using NOT (~) which inverts all bits
2. Which of the following C code snippets correctly clears bit 2 of a register named REG?
easy
A. REG = REG | (1 << 2);
B. REG = REG << 2;
C. REG = REG ^ (1 << 2);
D. REG = REG & ~(1 << 2);
Solution
Step 1: Understand clearing a bit
Clearing means setting the bit to 0 without changing others.
Step 2: Use AND with NOT mask
AND with NOT of (1 shifted to bit 2) clears bit 2.
Final Answer:
REG = REG & ~(1 << 2); -> Option D
Quick Check:
Clear bit = AND with NOT mask [OK]
Hint: Clear bits with AND and NOT mask (~) [OK]
Common Mistakes:
Using OR (|) which sets bits
Using XOR (^) which toggles bits
Using shift (<<) alone which moves bits
3. Given REG = 0b00001100;, what is the value of REG after executing REG ^= (1 << 3);?
medium
A. 0b00001100
B. 0b00000100
C. 0b00011100
D. 0b00001000
Solution
Step 1: Understand XOR toggling
XOR toggles the bit: if 1 becomes 0, if 0 becomes 1.
Step 2: Toggle bit 3 of REG
Bit 3 is currently 1 (0b00001100). Toggling it changes it to 0, so REG becomes 0b00000100.
Final Answer:
0b00000100 -> Option B
Quick Check:
XOR toggles bit 3 from 1 to 0 [OK]
Hint: XOR (^) flips bits: 1->0 or 0->1 [OK]
Common Mistakes:
Thinking XOR sets bit instead of toggling
Confusing bit positions (count from 0)
Not converting binary correctly
4. What is wrong with this code to check if bit 4 of REG is set?
if (REG & (1 << 4) == 1) {
// bit 4 is set
}
medium
A. The comparison '== 1' is incorrect; it should check if result is non-zero.
B. The shift operator '<<' is used wrongly; it should be '>>'.
C. The bit number 4 is out of range for an 8-bit register.
D. The code should use OR (|) instead of AND (&) to check bits.
Solution
Step 1: Understand bit check expression
REG & (1 << 4) returns non-zero if bit 4 is set, zero if not.
Step 2: Fix comparison
Comparing to 1 is wrong because bit 4 corresponds to value 16, not 1. Should check if result != 0.
Final Answer:
The comparison '== 1' is incorrect; it should check if result is non-zero. -> Option A
Quick Check:
Check bits by testing non-zero result [OK]
Hint: Check bits by testing if (REG & mask) != 0 [OK]
Common Mistakes:
Comparing bit check result to 1 instead of non-zero
Using shift right (>>) instead of left (<<)
Using OR (|) to check bits which sets bits
5. You want to toggle bits 0, 2, and 5 of an 8-bit register REG simultaneously. Which code correctly does this?