0
0
Embedded Cprogramming~10 mins

OR for setting bits in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - OR for setting bits
Start with initial value
Choose bit mask to set
Apply OR operation: value | mask
Result has bits set where mask has 1s
End
Start with a number, pick bits to set using a mask, then use OR to turn those bits on without changing others.
Execution Sample
Embedded C
unsigned char value = 0x12;  // 00010010 in binary
unsigned char mask = 0x05;   // 00000101 in binary
value = value | mask;         // Set bits using OR
This code sets bits 0 and 2 of 'value' using OR with 'mask'.
Execution Table
Stepvalue (binary)mask (binary)OperationResult (binary)Result (hex)
10001001000000101value | mask000101110x17
20001011100000101No further operation000101110x17
💡 After OR, bits in mask are set in value; no more changes.
Variable Tracker
VariableStartAfter ORFinal
value0x12 (00010010)0x17 (00010111)0x17 (00010111)
mask0x05 (00000101)0x05 (00000101)0x05 (00000101)
Key Moments - 3 Insights
Why does OR set bits without clearing any existing bits?
Because OR with 1 always results in 1, so bits in mask turn on bits in value, but OR with 0 leaves bits unchanged (see execution_table step 1).
What happens to bits in value that are 1 but 0 in mask?
They stay 1 because OR with 0 keeps the original bit (see execution_table step 1, bits 4 and 1).
Why do we use OR instead of AND to set bits?
AND can only clear bits (set to 0), but OR can set bits to 1 without affecting others (see concept_flow).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'value' after the OR operation?
A0x17
B0x12
C0x05
D0x00
💡 Hint
Check the 'Result (hex)' column in execution_table step 1.
At which step does the value stop changing?
AStep 1
BStep 3
CStep 2
DStep 0
💡 Hint
Look at execution_table rows where operation is 'No further operation'.
If mask was 0x08 (00001000), what bit would be set in value after OR?
ABit 0
BBit 3
CBit 2
DBit 1
💡 Hint
Mask bits correspond to positions of 1s in binary; 0x08 has bit 3 set.
Concept Snapshot
OR for setting bits:
- Use bitwise OR (|) to set bits.
- Syntax: value = value | mask;
- Bits set to 1 in mask turn on those bits in value.
- Bits 0 in mask leave value bits unchanged.
- Useful to turn on flags without changing others.
Full Transcript
This visual trace shows how bitwise OR sets bits in a value. We start with an initial value 0x12 (binary 00010010). We choose a mask 0x05 (binary 00000101) to set bits 0 and 2. Applying OR combines bits: any bit set in mask becomes set in value. After OR, value becomes 0x17 (binary 00010111), with bits 0, 1, 2, and 4 set. The variable tracker shows value changing from 0x12 to 0x17, while mask stays the same. Key moments clarify why OR sets bits without clearing others and why AND is not used here. The quiz tests understanding of the value after OR, when changes stop, and which bit corresponds to a mask value. This helps beginners see how OR sets bits step-by-step.