0
0
Embedded Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Clearing a specific bit in a register
Start with register value
Create mask with 1 at bit position
Invert mask to have 0 at bit position
AND register with inverted mask
Result: bit cleared, others unchanged
End
We create a mask with a 1 at the bit to clear, invert it, then AND with the register to clear that bit.
Execution Sample
Embedded C
unsigned char reg = 0b11111111;
unsigned char bit = 4;
reg &= ~(1 << bit);
This code clears bit 4 of the register reg.
Execution Table
StepActionValue/ExpressionResult
1Initial register valuereg0b11111111 (255 decimal)
2Bit to clearbit4
3Create mask with 1 at bit 41 << bit0b00010000 (16 decimal)
4Invert mask~(1 << bit)0b11101111 (239 decimal)
5AND register with inverted maskreg & 0b111011110b11111111 & 0b11101111 = 0b11101111 (239 decimal)
6Assign result back to regreg &= ~(1 << bit)reg = 0b11101111 (239 decimal)
7Check if bit 4 clearedbit 4 of reg0 (cleared)
8ExitNo more stepsBit cleared successfully
💡 Bit 4 cleared by ANDing with inverted mask, execution ends.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
reg0b111111110b111111110b111111110b111011110b11101111
bit44444
maskN/A0b000100000b11101111N/AN/A
Key Moments - 3 Insights
Why do we invert the mask before ANDing with the register?
Inverting the mask changes the bit we want to clear from 1 to 0 and all others to 1, so when ANDed, only that bit is cleared (set to 0) while others stay the same. See execution_table step 4 and 5.
What happens if we don't invert the mask and just AND with (1 << bit)?
ANDing with (1 << bit) would clear all bits except the one we want to clear, which is wrong. We must invert the mask to keep other bits unchanged. Refer to execution_table step 5.
Why use bit shifting (1 << bit) to create the mask?
Bit shifting moves the 1 to the exact bit position we want to clear, making the mask precise. This is shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of the mask after step 3?
A0b00010000
B0b11101111
C0b11111111
D0b00001111
💡 Hint
Check the 'Value/Expression' column at step 3 in execution_table.
At which step does the register get updated with the cleared bit?
AStep 5
BStep 6
CStep 3
DStep 7
💡 Hint
Look for the step where reg is assigned the new value in execution_table.
If we change bit from 4 to 3, how would the mask at step 3 change?
A0b00000100
B0b00010000
C0b00001000
D0b00000010
💡 Hint
Recall that 1 << bit shifts 1 left by bit positions; see variable_tracker mask values.
Concept Snapshot
Clearing a bit in a register:
Use mask = 1 << bit_position
Invert mask: ~mask
AND register with inverted mask: reg &= ~mask
This clears only the target bit, others stay unchanged.
Full Transcript
This visual trace shows how to clear a specific bit in a register using bitwise operations. We start with a register value and a bit position to clear. We create a mask by shifting 1 to the bit position, then invert it to have 0 at that bit and 1 elsewhere. ANDing the register with this inverted mask clears the target bit while keeping others unchanged. The execution table walks through each step with values, and the variable tracker shows how variables change. Key moments clarify why inversion is needed and how shifting works. The quiz tests understanding of mask creation, register update, and bit position changes.