Bird
Raised Fist0
DSA C#programming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Set Clear Toggle a Specific Bit
Start with number
↓
Choose bit position
↓
Select operation: Set / Clear / Toggle
↓
Apply bitwise mask
↓
Update number with new bit value
↓
Resulting number with bit changed
This flow shows how to pick a bit position and apply set, clear, or toggle operation using bitwise masks to update the number.
Execution Sample
DSA C#
int num = 13; // binary 1101
int pos = 2;
// Set bit at pos
num = num | (1 << pos);
Console.WriteLine(num);
This code sets the bit at position 2 (0-based from right) in number 13.
Execution Table
StepOperationBit PositionMask (binary)Number Before (binary)Number After (binary)Resulting Number
1Start--1101110113
2Calculate mask201001101110113
3Set bit (num | mask)201001101110113
4Result---110113
5Clear bit (num & ~mask)20100110110019
6Toggle bit (num ^ mask)20100110110019
7Toggle bit again (num ^ mask)201001001110113
8End-----
💡 All operations done for bit position 2 on initial number 13.
Variable Tracker
VariableStartAfter SetAfter ClearAfter Toggle 1After Toggle 2Final
num (decimal)1313991313
num (binary)110111011001100111011101
pos222222
mask (binary)010001000100010001000100
Key Moments - 3 Insights
Why does setting a bit use OR (|) with the mask?
Because OR with 1 at the bit position ensures that bit becomes 1 without changing other bits, as shown in execution_table step 3.
Why do we clear a bit using AND (&) with the negated mask (~mask)?
AND with 0 at the bit position clears that bit to 0, while keeping others unchanged, as seen in step 5 of execution_table.
How does toggling a bit with XOR (^) work?
XOR flips the bit: if it was 1 it becomes 0, if 0 it becomes 1. This is shown in steps 6 and 7 where toggling twice returns to original.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the binary value of num after clearing bit 2?
A1101
B1001
C0100
D1111
💡 Hint
Check execution_table row 5 under 'Number After (binary)' column.
At which step does toggling bit 2 first change num from 1101 to 1001?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look at execution_table rows 6 and 7 for toggle operations.
If we change pos to 1, what would be the mask in binary?
A0001
B0100
C0010
D1000
💡 Hint
Mask is 1 shifted left by pos bits; see variable_tracker mask value for pos=2.
Concept Snapshot
Set, Clear, Toggle a bit at position pos in number num:
- Set: num = num | (1 << pos)
- Clear: num = num & ~(1 << pos)
- Toggle: num = num ^ (1 << pos)
Bit positions start at 0 from right (least significant bit).
Use bitwise masks to change only the target bit without affecting others.
Full Transcript
This lesson shows how to change a specific bit in a number using bitwise operations. We start with a number and pick a bit position. To set the bit, we use OR with a mask that has 1 at that position. To clear the bit, we use AND with the negation of that mask. To toggle, we use XOR with the mask. The execution table traces these steps on number 13 (binary 1101) at bit position 2. Variable tracker shows how num and mask change after each operation. Key moments clarify why each bitwise operation works. The quiz tests understanding of these changes and mask calculations.

Practice

(1/5)
1. What does the operation number |= (1 << position) do to the bit at position in number?
easy
A. Toggles the bit at position
B. Clears the bit at position to 0
C. Sets the bit at position to 1
D. Checks if the bit at position is set

Solution

  1. Step 1: Understand the bitwise OR with shifted 1

    The expression 1 << position creates a number with only the bit at position set to 1. Using OR (|=) with this sets that bit in number to 1 without changing other bits.
  2. Step 2: Effect on the bit at position

    Since OR with 1 always results in 1, the bit at position is set to 1 regardless of its previous value.
  3. Final Answer:

    Sets the bit at position to 1 -> Option C
  4. Quick Check:

    OR with 1 sets bit [OK]
Hint: Use OR with shifted 1 to set a bit [OK]
Common Mistakes:
  • Confusing OR with XOR for toggling
  • Thinking it clears the bit
  • Assuming it checks the bit instead
2. Which of the following C# statements correctly clears (sets to 0) the bit at position pos in an integer num?
easy
A. num &= ~(1 << pos);
B. num |= (1 << pos);
C. num ^= (1 << pos);
D. num = num << pos;

Solution

  1. Step 1: Understand clearing a bit

    To clear a bit, we use AND with the complement of a mask that has 1 at the target bit. The mask is 1 << pos, so complement is ~(1 << pos).
  2. Step 2: Apply AND with complemented mask

    Using num &= ~(1 << pos); clears the bit at pos while leaving others unchanged.
  3. Final Answer:

    num &= ~(1 << pos); -> Option A
  4. Quick Check:

    AND with complement clears bit [OK]
Hint: Use AND with NOT mask to clear a bit [OK]
Common Mistakes:
  • Using OR instead of AND to clear
  • Using XOR which toggles instead of clears
  • Using left shift alone which changes number
3. What is the output of the following C# code?
int num = 10; // binary 1010
int pos = 1;
num ^= (1 << pos);
Console.WriteLine(num);
medium
A. 10
B. 14
C. 12
D. 8

Solution

  1. Step 1: Understand initial number and bit position

    Number 10 in binary is 1010. Position 1 means the second bit from right (0-based). Bit at position 1 is 1.
  2. Step 2: Apply XOR to toggle bit at position 1

    XOR toggles the bit. Since bit at position 1 is 1, toggling it changes it to 0. New binary is 1000 which is decimal 8.
  3. Final Answer:

    8 -> Option D
  4. Quick Check:

    Toggle bit 1 in 1010 = 1000 = 8 [OK]
Hint: XOR toggles bit; 1 becomes 0, 0 becomes 1 [OK]
Common Mistakes:
  • Confusing XOR with OR or AND
  • Miscounting bit positions (0-based)
  • Assuming no change if bit is 1
4. The following code is intended to toggle the bit at position pos in num. What is the error?
int num = 5; // binary 0101
int pos = 2;
num = num & (1 << pos);
Console.WriteLine(num);
medium
A. It clears all bits except the one at pos
B. It toggles the bit correctly
C. It sets the bit at pos to 1
D. It causes a syntax error

Solution

  1. Step 1: Analyze the operation used

    The code uses AND (&) with 1 << pos. This keeps only the bit at pos if it was set, clearing all others.
  2. Step 2: Compare with toggle operation

    Toggling requires XOR (^), not AND. So this code does not toggle but masks all bits except the one at pos.
  3. Final Answer:

    It clears all bits except the one at pos -> Option A
  4. Quick Check:

    AND with mask keeps only that bit [OK]
Hint: Use XOR to toggle, AND masks bits [OK]
Common Mistakes:
  • Using AND instead of XOR for toggle
  • Expecting toggle but getting masked value
  • Confusing bitwise operators
5. You have an integer num and want to toggle the bit at position pos only if it is currently set (1). Which code snippet correctly does this?
hard
A. if ((num ^ (1 << pos)) != 0) num ^= (1 << pos);
B. if ((num & (1 << pos)) != 0) num ^= (1 << pos);
C. if ((num & ~(1 << pos)) != 0) num ^= (1 << pos);
D. if ((num | (1 << pos)) != 0) num ^= (1 << pos);

Solution

  1. Step 1: Check if bit at pos is set

    Using num & (1 << pos) checks if bit at pos is 1. If result is not zero, bit is set.
  2. Step 2: Toggle bit only if set

    If bit is set, toggle it using XOR: num ^= (1 << pos);. This flips the bit from 1 to 0.
  3. Final Answer:

    if ((num & (1 << pos)) != 0) num ^= (1 << pos); -> Option B
  4. Quick Check:

    Check bit with AND, toggle with XOR [OK]
Hint: Check bit with AND before toggling with XOR [OK]
Common Mistakes:
  • Using OR instead of AND to check bit
  • Toggling without checking bit first
  • Using incorrect mask complement
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard