Bird
Raised Fist0
Embedded Cprogramming~10 mins

Setting a specific bit in a register in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Setting a specific bit in a register
Start
↓
Identify bit position
↓
Create mask: 1 << bit_position
↓
Apply mask with OR to register
↓
Bit set in register
↓
End
This flow shows how to set a specific bit by creating a mask and using OR operation on the register.
Execution Sample
Embedded C
unsigned int reg = 0x00;
int bit_pos = 3;
reg = reg | (1 << bit_pos);
This code sets bit 3 of the register reg to 1.
Execution Table
StepVariableValueOperationResult
1reg0x00 (00000000)Startreg initialized to 0
2bit_pos3Startbit position to set is 3
3mask1 << 3 = 0x08 (00001000)Create maskmask has bit 3 set
4reg0x00 | 0x08OR operationreg becomes 0x08 (00001000)
5reg0x08EndBit 3 is set in reg
💡 Bit 3 set successfully, operation complete
Variable Tracker
VariableStartAfter Step 3After Step 4Final
reg0x000x000x080x08
bit_pos3333
maskN/A0x080x080x08
Key Moments - 3 Insights
Why do we use (1 << bit_pos) to create the mask?
Because shifting 1 left by bit_pos moves the single 1 bit to the correct position to set that bit in the register, as shown in step 3 of the execution_table.
Why do we use the OR operator (|) to set the bit?
OR keeps all existing bits unchanged and sets the target bit to 1, as seen in step 4 where reg changes from 0x00 to 0x08.
What happens if the bit is already set?
Using OR again won't change the register because OR with 1 keeps the bit set, so the register value remains the same, ensuring no unintended changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the binary value of the mask?
A00000100
B00010000
C00001000
D00000001
💡 Hint
Check the 'Value' column at step 3 in execution_table where mask is 0x08 (00001000).
At which step does the register value change from 0x00 to 0x08?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Operation' and 'Result' columns in execution_table to see when reg changes.
If bit_pos was 1 instead of 3, what would the mask value be at step 3?
A00000010
B00000100
C00001000
D00000001
💡 Hint
Recall mask is 1 shifted left by bit_pos; for bit_pos=1, mask is 1 << 1 = 0x02 (00000010).
Concept Snapshot
Setting a specific bit in a register:
- Use mask = 1 << bit_position
- Use OR: register = register | mask
- This sets only the target bit to 1
- Other bits remain unchanged
- Common in embedded C for hardware control
Full Transcript
This visual execution shows how to set a specific bit in a register using embedded C. We start with a register value of 0. We pick the bit position to set, here bit 3. We create a mask by shifting 1 left by the bit position, resulting in 0x08 (binary 00001000). Then we use the OR operator to set that bit in the register. The register changes from 0x00 to 0x08, meaning bit 3 is now set. This method keeps all other bits unchanged. Key points are creating the mask with shift and using OR to set the bit safely. The quizzes check understanding of mask creation, when the register changes, and how changing bit position affects the mask.

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

  1. 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.
  2. Step 2: Apply bitwise OR to set bit

    Using |= with this mask sets bit 3 to 1 in REGISTER without changing other bits.
  3. Final Answer:

    Sets the 4th bit (bit position 3) of REGISTER to 1 without changing other bits -> Option B
  4. 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

  1. Step 1: Identify correct operator to set bit

    To set a bit without affecting others, use bitwise OR assignment |= with a mask.
  2. Step 2: Check the bit mask and syntax

    1 << 5 creates a mask for bit 5. Combining with |= sets that bit.
  3. Final Answer:

    PORTA |= (1 << 5); -> Option D
  4. 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

  1. Step 1: Convert initial REG value to binary

    0x12 in binary is 00010010. Bit positions (0 to 7) from right to left.
  2. Step 2: Calculate mask and apply OR

    1 << 4 is 00010000 (bit 4 set). OR with 00010010 results in 00110010 (0x32).
  3. Final Answer:

    0x32 -> Option A
  4. 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

  1. Step 1: Analyze bitwise AND usage

    The code uses & with mask (1 << 2), which clears all bits except bit 2.
  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.
  3. Final Answer:

    Using & instead of | clears all bits except bit 2 -> Option A
  4. 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

  1. 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.
  2. Step 2: Use |= to set bits without clearing others

    Using |= applies the mask to REG16, setting those bits while preserving others.
  3. Final Answer:

    REG16 |= (1 << 0) | (1 << 7) | (1 << 15); -> Option C
  4. Quick Check:

    Combine masks with | and set with |= [OK]
Hint: Combine bit masks with | and set with |= [OK]
Common Mistakes:
  • Using & instead of | to combine masks
  • Using = overwrites all bits, losing previous data
  • Using & instead of | in assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard