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 operation
↓
Update number with new bit value
↓
Resulting number with bit changed
This flow shows how to change a specific bit in a number by choosing the bit position and operation, then applying the bitwise operation to update the number.
Execution Sample
DSA C++
int num = 13; // binary 1101
int pos = 1;
// Set bit at pos
num = num | (1 << pos);
This code sets the bit at position 1 (0-based from right) in number 13.
Execution Table
StepOperationBit PositionBit MaskNumber BeforeNumber AfterBinary BeforeBinary After
1Start--131311011101
2Set bit10010131511011111
3Clear bit20100151111111011
4Toggle bit00001111010111010
5End--101010101010
💡 All operations applied; final number is 10 (binary 1010).
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
num13 (1101)15 (1111)11 (1011)10 (1010)10 (1010)
pos-120-
bit_mask-2 (0010)4 (0100)1 (0001)-
Key Moments - 3 Insights
Why do we use (1 << pos) to create the bit mask?
Because shifting 1 left by pos creates a mask with only that bit set, as shown in execution_table rows 2-4 under 'Bit Mask'.
Why does setting a bit use OR (|) operation?
OR with the mask sets the bit to 1 without changing other bits, as seen in step 2 where 1101 | 0010 = 1111.
How does toggling a bit work with XOR (^)?
XOR flips the bit if mask bit is 1, else leaves it unchanged, demonstrated in step 4 where 1011 ^ 0001 = 1010.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the binary representation of the number after clearing bit 2?
A1111
B1011
C1101
D1010
💡 Hint
Check the 'Binary After' column in row with Step 3.
At which step does the number change from 15 to 11?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at 'Number Before' and 'Number After' columns in execution_table rows.
If we toggle bit at position 1 instead of 0 in step 4, what would be the new binary after toggle?
A1111
B1010
C1001
D1101
💡 Hint
Toggle flips the bit at given position; check how XOR works with bit mask in variable_tracker.
Concept Snapshot
Set, Clear, Toggle a bit:
- Use bit mask = 1 << position
- Set bit: num = num | mask
- Clear bit: num = num & ~mask
- Toggle bit: num = num ^ mask
- Position counts from 0 (rightmost bit)
- Bitwise ops change only targeted bit
Full Transcript
This lesson shows how to change a specific bit in a number using bitwise operations. We start with a number and choose a bit position. We create a mask by shifting 1 left by that position. To set a bit, we OR the number with the mask. To clear a bit, we AND the number with the negation of the mask. To toggle a bit, we XOR the number with the mask. The execution table traces these steps with binary and decimal values. The variable tracker shows how variables change after each operation. Key moments clarify why we use shifts and bitwise operators. The quiz tests understanding of the bit changes at each step.

Practice

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

Solution

  1. Step 1: Understand the bit shift operation

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

    Using |= with this mask 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 B
  4. Quick Check:

    num |= (1 << k) sets bit k [OK]
Hint: Use OR with shifted 1 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 is the correct syntax to clear the 3rd bit (0-indexed) of an integer variable num in C++?
easy
A. num &= ~(1 << 3);
B. num |= (1 << 3);
C. num ^= (1 << 3);
D. num = (num & (1 << 3));

Solution

  1. Step 1: Create mask to clear bit

    1 << 3 sets only the 3rd bit. Negating it with ~ creates a mask with all bits 1 except the 3rd bit 0.
  2. Step 2: Use AND assignment to clear bit

    Applying &= with this mask clears the 3rd bit of num while keeping others unchanged.
  3. Final Answer:

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

    AND with negated mask clears bit [OK]
Hint: Use AND with negated 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
num ^= (1 << 1);
std::cout << num << std::endl;
medium
A. 8
B. 10
C. 12
D. 14

Solution

  1. Step 1: Understand initial value and bit positions

    num = 10 in binary is 1010. Bits are indexed from 0 (rightmost). Bit 1 is currently 1.
  2. Step 2: Toggle the 1st bit using XOR

    num ^= (1 << 1) toggles bit 1. Since bit 1 is 1, toggling it changes it to 0. New binary: 1000 (decimal 8).
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    Toggle bit 1 of 1010 = 1000 = 8 [OK]
Hint: XOR toggles bits; 1 flips, 0 stays same [OK]
Common Mistakes:
  • Confusing toggle with set or clear
  • Miscounting bit positions starting at 1 instead of 0
  • Assuming XOR always sets bit
4. Identify the error in the following code snippet that attempts to clear the 2nd bit of num:
int num = 15;
num = num & (1 << 2);
std::cout << num << std::endl;
medium
A. It sets only the 2nd bit instead of clearing it
B. It toggles the 2nd bit instead of clearing it
C. It clears all bits except the 2nd bit
D. It causes a syntax error due to missing parentheses

Solution

  1. Step 1: Analyze the mask used

    The mask (1 << 2) has only the 2nd bit set to 1, all others 0.
  2. Step 2: Understand the effect of AND with mask

    ANDing num with this mask keeps only the 2nd bit of num and clears all others. This is opposite of clearing the 2nd bit.
  3. Final Answer:

    It clears all bits except the 2nd bit -> Option C
  4. Quick Check:

    AND with mask keeps bit, does not clear it [OK]
Hint: Use AND with negated mask to clear bits, not plain mask [OK]
Common Mistakes:
  • Using mask directly instead of negated mask
  • Confusing AND with OR for clearing bits
  • Assuming AND with mask clears bit
5. Given an integer num, which expression correctly toggles the 0th bit only if it is currently set (1), otherwise leaves num unchanged?
hard
A. num ^= 1;
B. num &= ~1;
C. num |= 1;
D. if ((num & 1) == 1) num ^= 1;

Solution

  1. Step 1: Check if 0th bit is set

    The condition (num & 1) == 1 checks if the least significant bit is 1.
  2. Step 2: Toggle 0th bit only if set

    If the bit is set, num ^= 1 toggles it to 0. Otherwise, no change is made.
  3. Final Answer:

    if ((num & 1) == 1) num ^= 1; -> Option D
  4. Quick Check:

    Toggle only if bit set using condition [OK]
Hint: Check bit before toggling to avoid unwanted changes [OK]
Common Mistakes:
  • Toggling bit unconditionally
  • Using OR or AND which always set or clear bit
  • Not checking bit state before 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