Bird
Raised Fist0
DSA Cprogramming~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
↓
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.

Practice

(1/5)
1. What does the operation num |= (1 << k) do to the integer num?
easy
A. Toggles the k-th bit of num
B. Clears the k-th bit of num to 0
C. Checks if the k-th bit of num is set
D. Sets the k-th bit of num to 1

Solution

  1. Step 1: Understand the bit shift operation

    1 << k creates a number with only the k-th bit set to 1 and others 0.
  2. Step 2: Apply bitwise OR to set the bit

    Using num |= (1 << k) sets the k-th bit of num to 1 without changing other bits.
  3. Final Answer:

    Sets the k-th bit of num to 1 -> Option D
  4. Quick Check:

    Set bit = OR with 1 shifted [OK]
Hint: Use OR with 1 shifted to set a bit [OK]
Common Mistakes:
  • Confusing set with clear operation
  • Using AND instead of OR to set bits
  • Misunderstanding bit positions starting from 0
2. Which of the following C statements correctly clears the k-th bit of an integer num?
easy
A. num = num << k;
B. num &= ~(1 << k);
C. num ^= (1 << k);
D. num |= (1 << k);

Solution

  1. Step 1: Understand clearing a bit

    Clearing means setting the bit to 0. We use AND with the complement of the bit mask.
  2. Step 2: Analyze the options

    num &= ~(1 << k); clears the k-th bit by ANDing with all 1s except 0 at k-th bit.
  3. Final Answer:

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

    Clear bit = AND with NOT of shifted 1 [OK]
Hint: Use AND with NOT of shifted 1 to clear bit [OK]
Common Mistakes:
  • Using OR instead of AND to clear bits
  • Forgetting to negate the mask
  • Using XOR which toggles instead of clears
3. What is the output of the following C code?
int num = 10; // binary 1010
int k = 1;
num ^= (1 << k);
printf("%d", num);
medium
A. 12
B. 10
C. 8
D. 14

Solution

  1. Step 1: Understand initial value and bit positions

    num = 10 (binary 1010), k = 1 means second bit from right (0-based).
  2. Step 2: Apply XOR to toggle the k-th bit

    Toggle bit 1: bit at position 1 is 1 in 1010, toggling it changes to 0, so binary becomes 1000 which is 8.
  3. Final Answer:

    8 -> Option C
  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 toggle with set or clear
  • Miscounting bit positions starting from 1 instead of 0
  • Assuming XOR always sets bit
4. The following code is intended to clear the 3rd bit of num. What is the error?
int num = 15; // binary 1111
int k = 3;
num = num & (1 << k);
medium
A. It clears all bits except the 3rd bit
B. It sets the 3rd bit instead of clearing
C. It toggles the 3rd bit instead of clearing
D. No error, code works correctly

Solution

  1. Step 1: Analyze the bitwise AND operation

    The code does num & (1 << k), which keeps only the k-th bit and clears others.
  2. Step 2: Understand the intended operation

    To clear the k-th bit, we must AND with the complement of the bit mask, not the mask itself.
  3. Final Answer:

    It clears all bits except the 3rd bit -> Option A
  4. Quick Check:

    AND with mask keeps only that bit, not clears it [OK]
Hint: Clear bit = AND with NOT of mask, not mask alone [OK]
Common Mistakes:
  • Using mask instead of its complement to clear bit
  • Confusing AND with OR for clearing bits
  • Assuming AND with mask clears bit
5. You want to toggle the 0th and 2nd bits of an integer num simultaneously. Which expression correctly does this in C?
hard
A. num ^= (1 << 0) | (1 << 2);
B. num |= (1 << 0) & (1 << 2);
C. num &= ~( (1 << 0) | (1 << 2) );
D. num = (num << 0) ^ (num << 2);

Solution

  1. Step 1: Understand toggling multiple bits

    Toggling bits uses XOR. To toggle multiple bits, XOR with mask having those bits set.
  2. Step 2: Create mask for bits 0 and 2

    Mask is (1 << 0) | (1 << 2), which sets bits 0 and 2 to 1.
  3. Step 3: Apply XOR with mask

    Expression num ^= (1 << 0) | (1 << 2); toggles both bits simultaneously.
  4. Final Answer:

    num ^= (1 << 0) | (1 << 2); -> Option A
  5. Quick Check:

    XOR with combined mask toggles multiple bits [OK]
Hint: XOR with ORed shifted bits toggles multiple bits [OK]
Common Mistakes:
  • Using AND or OR instead of XOR for toggling
  • Incorrectly combining masks with AND
  • Misusing shift operations in toggle
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