0
0
Cprogramming~10 mins

Bit manipulation techniques - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Bit manipulation techniques
Start with integer x
Choose bit operation
Set
Use bitwise operators
Update x with new value
Use or print result
End
Start with a number, pick a bit operation (set, clear, toggle), apply bitwise operators, update the number, then use or print the result.
Execution Sample
C
int x = 5; // binary 0101
x = x | (1 << 1); // set bit 1
printf("%d", x);
This code sets bit 1 of x (which is 5) and prints the new value.
Execution Table
StepOperationExpressionBinary BeforeBinary AfterDecimal AfterExplanation
1Initialize xx = 500000101000001015Start with x = 5 (binary 0101)
2Set bit 1x = x | (1 << 1)00000101000001117Set bit 1: 0101 OR 0010 = 0111
3Print xprintf("%d", x)00000111000001117Output the updated value 7
💡 All steps completed; bit 1 set successfully.
Variable Tracker
VariableStartAfter Step 2Final
x5 (00000101)7 (00000111)7 (00000111)
Key Moments - 3 Insights
Why do we use (1 << 1) to set bit 1?
Because (1 << 1) shifts 1 to the left by 1 position, creating a mask with only bit 1 set (binary 0010). This mask is used with OR to set that bit in x (see execution_table step 2).
Why does OR operation set the bit instead of clearing it?
OR operation sets a bit to 1 if either operand has 1 in that bit. So when OR-ing with a mask that has bit 1 set, that bit in x becomes 1 regardless of its previous value (execution_table step 2).
What happens if the bit is already set before OR?
If the bit is already 1, OR-ing with 1 keeps it 1, so the value stays the same (no change). This is why OR is safe to use to set bits without affecting others.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the binary value of x after step 2?
A00000010
B00000111
C00000101
D00000001
💡 Hint
Check the 'Binary After' column in execution_table row for step 2.
At which step does x change from 5 to 7?
AStep 2
BStep 1
CStep 3
DNo change
💡 Hint
Look at the 'Decimal After' column and see when it changes from 5 to 7.
If we wanted to clear bit 2 instead of setting bit 1, which operation would we use?
Ax = x | (1 << 2)
Bx = x ^ (1 << 2)
Cx = x & ~(1 << 2)
Dx = x << 2
💡 Hint
Clearing a bit uses AND with the negated mask; see common bit manipulation patterns.
Concept Snapshot
Bit manipulation uses bitwise operators to change bits.
Set bit: x = x | (1 << n)
Clear bit: x = x & ~(1 << n)
Toggle bit: x = x ^ (1 << n)
Shift left/right moves bits.
Use masks to target specific bits.
Full Transcript
This lesson shows how to change bits in an integer using bitwise operators in C. We start with a number x, then pick an operation like setting a bit. To set bit 1, we create a mask by shifting 1 left by 1 (1 << 1), which is binary 0010. We then OR this mask with x to set that bit. The execution table shows x starting at 5 (binary 0101), then after setting bit 1, it becomes 7 (binary 0111). We print the result. Key points include why shifting creates the mask, how OR sets bits, and what happens if the bit is already set. The quiz asks about the binary value after setting the bit, when the change happens, and how to clear a bit using AND with negated mask. The snapshot summarizes common bit manipulation patterns for setting, clearing, toggling bits, and shifting.