0
0
Embedded Cprogramming~10 mins

Register bit manipulation patterns in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Register bit manipulation patterns
Start with Register Value
Choose Bit Position
Select Operation
Set Bit
Update Register with New Value
End
This flow shows how to pick a bit in a register and either set, clear, or toggle it, then update the register.
Execution Sample
Embedded C
uint8_t reg = 0b00001100;
uint8_t bit_pos = 2;
reg |= (1 << bit_pos); // Set bit 2
reg &= ~(1 << 3);      // Clear bit 3
reg ^= (1 << 0);       // Toggle bit 0
This code sets bit 2, clears bit 3, and toggles bit 0 of an 8-bit register.
Execution Table
StepOperationBit PositionRegister BeforeMask UsedRegister AfterExplanation
1Set Bit2000011000000010000001100Bit 2 is already set, register unchanged
2Clear Bit3000011001111011100000100Bit 3 cleared from 1 to 0
3Toggle Bit0000001000000000100000101Bit 0 toggled from 0 to 1
4End-00000101--All operations done, final register value
💡 All bit manipulation operations completed on the register.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
reg0000110000001100000001000000010100000101
bit_pos22222
Key Moments - 3 Insights
Why does setting bit 2 not change the register in step 1?
Because bit 2 was already 1 in the register before step 1, so OR with 1<<2 keeps it the same (see execution_table row 1).
Why do we use ~(1 << bit_pos) to clear a bit?
Because ~(1 << bit_pos) creates a mask with all bits 1 except the target bit 0, so ANDing clears that bit (see execution_table row 2).
How does toggling a bit work with XOR?
XOR with 1 flips the bit: 0 becomes 1, 1 becomes 0. Here, toggling bit 0 changed it from 0 to 1 (see execution_table row 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the register value after clearing bit 3?
A00000000
B00001100
C00000100
D00000101
💡 Hint
Check execution_table row 2 under 'Register After' column.
At which step does the register change from 00000100 to 00000101?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table rows 3 and 4 for register values.
If we toggle bit 2 instead of bit 0 in step 3, what would the register be after step 3?
A00000000
B00000101
C1
D-1
💡 Hint
Toggling bit 2 flips bit 2 from 1 to 0 on register 00000100 (see variable_tracker).
Concept Snapshot
Register bit manipulation patterns:
- Set bit: reg |= (1 << pos)
- Clear bit: reg &= ~(1 << pos)
- Toggle bit: reg ^= (1 << pos)
Use masks to change only one bit without affecting others.
Common in embedded C for hardware control.
Full Transcript
This lesson shows how to change individual bits in a register using bitwise operations. We start with a register value and pick a bit position. To set a bit, we OR the register with a mask that has 1 at that bit. To clear a bit, we AND with a mask that has 0 at that bit and 1s elsewhere. To toggle a bit, we XOR with a mask that has 1 at that bit. The execution table traces each step showing register values before and after each operation. Key moments explain why masks work and how bits change. The quiz tests understanding of register values at each step. This is essential for embedded programming to control hardware bits safely and efficiently.