📖 Scenario: You are working with a microcontroller and need to control hardware by setting and clearing bits in a register. Registers are like small boxes of switches, where each switch controls a feature.
🎯 Goal: Learn how to create a register variable, define bit masks, set and clear bits using bitwise operations, and finally print the register value to see the changes.
📋 What You'll Learn
Create an 8-bit register variable with an initial value
Define bit masks for specific bits
Use bitwise OR to set bits
Use bitwise AND with NOT to clear bits
Print the register value in hexadecimal format
💡 Why This Matters
🌍 Real World
Microcontrollers use registers to control hardware features like LEDs, motors, and sensors by turning bits on or off.
💼 Career
Embedded software engineers often manipulate register bits to configure and control hardware devices efficiently.
Progress0 / 4 steps
1
Create the register variable
Create an 8-bit unsigned integer variable called REG and set it to 0x00.
Embedded C
Hint
Use uint8_t to create an 8-bit register variable and set it to zero.
2
Define bit masks for bits 0 and 3
Define two constants called BIT0 and BIT3 with values 0x01 and 0x08 respectively.
Embedded C
Hint
Use #define to create bit masks for bit 0 and bit 3.
3
Set bit 0 and clear bit 3 in the register
Use bitwise OR to set bit 0 in REG with BIT0. Then use bitwise AND with NOT to clear bit 3 in REG with BIT3.
Embedded C
Hint
Use |= to set a bit and &= ~ to clear a bit.
4
Print the register value
Use printf to display the value of REG in hexadecimal format with the text "Register value: 0x" before it.
Embedded C
Hint
Use printf("Register value: 0x%02X\n", REG); to print the register in hex.
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?