Bird
0
0
DSA Cprogramming~10 mins

Set Clear Toggle a Specific Bit in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Set Clear Toggle a Specific Bit
Start with number
Choose bit position
Select operation: Set / Clear / Toggle
Create mask with 1 shifted to bit position
Apply operation using bitwise operator
Update number with new bit value
Result: number with bit changed
This flow shows how to change a specific bit in a number by creating a mask and applying set, clear, or toggle operations.
Execution Sample
DSA C
int num = 13; // binary 1101
int pos = 1; // bit position (0-based)
// Set bit
num = num | (1 << pos);
This code sets the bit at position 1 of number 13 (binary 1101).
Execution Table
StepOperationNumber (decimal)Number (binary)Mask (binary)Bitwise OperationResult (decimal)Result (binary)
1Start131101--131101
2Create mask--0010---
3Set bit13110100101101 OR 0010151111
4Clear bit15111100101111 AND NOT 0010131101
5Toggle bit13110100101101 XOR 0010151111
6Toggle bit again15111100101111 XOR 0010131101
7End131101----
💡 All operations complete; bit at position 1 has been set, cleared, and toggled as shown.
Variable Tracker
VariableStartAfter SetAfter ClearAfter Toggle 1After Toggle 2Final
num13 (1101)15 (1111)13 (1101)15 (1111)13 (1101)13 (1101)
pos111111
mask-2 (0010)2 (0010)2 (0010)2 (0010)2 (0010)
Key Moments - 3 Insights
Why do we use (1 << pos) to create the mask?
Because shifting 1 left by pos moves the single 1 bit to the correct position to target that bit. See execution_table step 2 where mask is 0010 for pos=1.
Why does clearing a bit use AND with NOT mask?
AND with NOT mask turns the target bit to 0 while keeping others unchanged. See execution_table step 4 where 1111 AND NOT 0010 results in 1101.
How does toggling a bit work with XOR?
XOR flips the bit if mask bit is 1, else leaves it. See steps 5 and 6 where toggling flips bit 1 between 1 and 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the decimal value of num after setting bit at position 1?
A13
B15
C14
D12
💡 Hint
Check execution_table row 3 under Result (decimal).
At which step does the bit at position 1 get cleared?
AStep 4
BStep 3
CStep 5
DStep 6
💡 Hint
Look for 'Clear bit' operation in execution_table.
If pos was 2 instead of 1, what would the mask binary be at step 2?
A0001
B0010
C0100
D1000
💡 Hint
Mask is 1 shifted left by pos bits; for pos=2, mask is 0100.
Concept Snapshot
Set, Clear, Toggle a bit:
- Create mask: 1 << pos
- Set bit: num | mask
- Clear bit: num & ~mask
- Toggle bit: num ^ mask
Bit positions start at 0 (rightmost bit).
Use bitwise ops to change only target bit.
Full Transcript
This concept shows how to change a specific bit in a number using bitwise operations. We create a mask by shifting 1 left by the bit position. To set a bit, we OR the number with the mask. To clear a bit, we AND the number with the NOT of the mask. To toggle a bit, we XOR the number with the mask. The execution table traces these steps on number 13 (binary 1101) at bit position 1, showing how the number changes after each operation.