0
0
Embedded Cprogramming~10 mins

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

Choose your learning style9 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.