📖 Scenario: You are working with a microcontroller and need to control hardware by setting bits in a register. Each bit in the register controls a different feature. You want to turn on a specific feature by setting its bit to 1 without changing the other bits.
🎯 Goal: Learn how to set a specific bit in a register using bitwise operations in C.
📋 What You'll Learn
Create a variable called register_value with an initial value.
Create a variable called bit_position to specify which bit to set.
Use bitwise operations to set the bit at bit_position in register_value.
Print the updated register_value in hexadecimal format.
💡 Why This Matters
🌍 Real World
Setting bits in registers is common in embedded systems to control hardware features like turning on LEDs, enabling sensors, or configuring communication.
💼 Career
Embedded software engineers and firmware developers frequently use bitwise operations to manipulate hardware registers efficiently.
Progress0 / 4 steps
1
Create the register variable
Create an unsigned 8-bit variable called register_value and set it to 0x12.
Embedded C
Hint
Use uint8_t from stdint.h to create an 8-bit unsigned variable.
2
Create the bit position variable
Create an unsigned 8-bit variable called bit_position and set it to 4.
Embedded C
Hint
Use the same uint8_t type for bit_position.
3
Set the specific bit in the register
Use the bitwise OR operator to set the bit at bit_position in register_value. Update register_value with the new value.
Embedded C
Hint
Shift 1 left by bit_position and OR it with register_value.
4
Print the updated register value
Print the updated register_value in hexadecimal format using printf with the format specifier %#04x.
Embedded C
Hint
Use printf("Updated register value: %#04x\n", register_value); inside main().
Practice
(1/5)
1. What does the expression REGISTER |= (1 << 3); do in embedded C?
easy
A. Clears the 3rd bit (bit position 2) of REGISTER to 0
B. Sets the 4th bit (bit position 3) of REGISTER to 1 without changing other bits
C. Toggles all bits of REGISTER
D. Resets REGISTER to zero
Solution
Step 1: Understand bit shifting
The expression 1 << 3 shifts the number 1 three places to the left, resulting in binary 00001000, which targets bit position 3.
Step 2: Apply bitwise OR to set bit
Using |= with this mask sets bit 3 to 1 in REGISTER without changing other bits.
Final Answer:
Sets the 4th bit (bit position 3) of REGISTER to 1 without changing other bits -> Option B
Quick Check:
Bit shift + OR sets specific bit = A [OK]
Hint: Use |= with (1 << bit) to set that bit only [OK]
Common Mistakes:
Confusing bit positions starting from 1 instead of 0
Using & instead of | to set bits
Thinking it clears bits instead of setting
2. Which of the following is the correct syntax to set bit 5 of a register named PORTA?
easy
A. PORTA = 1 << 5;
B. PORTA ^= (1 << 5);
C. PORTA &= ~(1 << 5);
D. PORTA |= (1 << 5);
Solution
Step 1: Identify correct operator to set bit
To set a bit without affecting others, use bitwise OR assignment |= with a mask.
Step 2: Check the bit mask and syntax
1 << 5 creates a mask for bit 5. Combining with |= sets that bit.
Final Answer:
PORTA |= (1 << 5); -> Option D
Quick Check:
Use |= with shifted 1 to set bit [OK]
Hint: Use |= with (1 << bit) to set bit correctly [OK]
Common Mistakes:
Using = instead of |= overwrites all bits
Using &= ~ to clear bits, not set
Using ^= toggles bits instead of setting
3. Given unsigned char REG = 0x12; (binary 00010010), what is the value of REG after executing REG |= (1 << 4);?
medium
A. 0x32
B. 0x12
C. 0x16
D. 0x10
Solution
Step 1: Convert initial REG value to binary
0x12 in binary is 00010010. Bit positions (0 to 7) from right to left.
Step 2: Calculate mask and apply OR
1 << 4 is 00010000 (bit 4 set). OR with 00010010 results in 00110010 (0x32).
Final Answer:
0x32 -> Option A
Quick Check:
0x12 | 0x10 = 0x32 [OK]
Hint: Convert hex to binary, OR with mask, convert back [OK]
Common Mistakes:
Miscounting bit positions starting from 1
Confusing hex and decimal values
Using &= or ^= instead of |=
4. Identify the error in the following code snippet intended to set bit 2 of REG:
unsigned char REG = 0x00;
REG = REG & (1 << 2);
medium
A. Using & instead of | clears all bits except bit 2
B. Bit shift operator << is used incorrectly
C. REG should be declared as int, not unsigned char
D. No error, code correctly sets bit 2
Solution
Step 1: Analyze bitwise AND usage
The code uses & with mask (1 << 2), which clears all bits except bit 2.
Step 2: Understand effect on REG
Since REG starts at 0, AND with mask results in 0, not setting bit 2. To set bit 2, OR |= should be used.
Final Answer:
Using & instead of | clears all bits except bit 2 -> Option A
Quick Check:
Use |= to set bits, & clears bits [OK]
Hint: Use |= to set bits, & clears bits [OK]
Common Mistakes:
Confusing & and | operators
Assuming &= sets bits
Ignoring initial value of REG
5. You have a 16-bit register REG16 initially set to 0x0000. Write the correct statement to set bits 0, 7, and 15 simultaneously without affecting other bits.
hard
A. REG16 = (1 << 0) | (1 << 7) | (1 << 15);
B. REG16 = (1 << 0) & (1 << 7) & (1 << 15);
C. REG16 |= (1 << 0) | (1 << 7) | (1 << 15);
D. REG16 |= (1 << 0) & (1 << 7) & (1 << 15);
Solution
Step 1: Create mask for bits 0, 7, and 15
Use bitwise OR to combine masks: (1 << 0) | (1 << 7) | (1 << 15) sets bits 0, 7, and 15.
Step 2: Use |= to set bits without clearing others
Using |= applies the mask to REG16, setting those bits while preserving others.