Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Set Clear Toggle a Specific Bit in DSA Typescript - 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 Typescript
let num = 10; // binary 1010
let bitPos = 1;
// Set bit at position 1
num = num | (1 << bitPos);
console.log(num);
This code sets the bit at position 1 of number 10 (binary 1010), resulting in 10 (decimal 10).
Execution Table
StepOperationNumber (decimal)Number (binary)Mask (binary)Bitwise OperationResult (decimal)Result (binary)
1Start1000001010--1000001010
2Create mask--00000010---
3Set bit10000010100000001010 | 21000001010
4Clear bit10000010100000001010 & ~2800001000
5Toggle bit10000010100000001010 ^ 2800001000
6Toggle bit again800001000000000108 ^ 21000001010
7End1000001010--1000001010
💡 Operations stop after toggling bit twice, returning to original number.
Variable Tracker
VariableStartAfter SetAfter ClearAfter Toggle 1After Toggle 2Final
num10 (00001010)10 (00001010)8 (00001000)8 (00001000)10 (00001010)10 (00001010)
mask-2 (00000010)2 (00000010)2 (00000010)2 (00000010)2 (00000010)
Key Moments - 3 Insights
Why does setting a bit with OR not change the number if the bit is already 1?
Because OR with 1 keeps the bit as 1, so if the bit was already set, the number stays the same (see Step 3 in execution_table).
Why do we use NOT (~) when clearing a bit?
We invert the mask so that all bits are 1 except the target bit, then AND keeps all bits except clears the target bit (see Step 4).
How does toggling a bit twice return the number to original?
XOR flips the bit each time; toggling twice flips it and flips it back (see Steps 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the decimal result after clearing the bit?
A10
B8
C2
D0
💡 Hint
Check the 'Result (decimal)' column at Step 4 in execution_table.
At which step does the number first change from 10 to 8?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Number (decimal)' and 'Result (decimal)' columns to see when the value changes.
If we toggle the bit at position 1 twice starting from 10, what is the final number?
A10
B8
C2
D0
💡 Hint
Refer to Steps 5 and 6 in execution_table and variable_tracker for num values.
Concept Snapshot
Set, Clear, Toggle a bit:
- Use mask = 1 << bitPos
- Set: num | mask
- Clear: num & ~mask
- Toggle: num ^ mask
- Bit positions start at 0 (rightmost bit)
- Operations change only the 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 to the bit position. To set a bit, we use OR with the mask. To clear a bit, we use AND with the inverted mask. To toggle a bit, we use XOR with the mask. The execution table traces these operations step-by-step on the number 10 (binary 1010) at bit position 1. Setting the bit keeps the number the same if the bit is already 1. Clearing the bit changes the number to 8 (binary 1000). Toggling flips the bit and toggling twice returns to the original number. The variable tracker shows how 'num' and 'mask' change after each operation. Key moments clarify common confusions about these bitwise operations. The visual quiz tests understanding of the steps and results.

Practice

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

Solution

  1. Step 1: Understand bitwise OR with shifted 1

    The expression 1 << pos creates a number with only the bit at position pos set to 1.
  2. Step 2: Apply OR operation to set bit

    Using num | (1 << pos) sets the bit at pos to 1 without changing other bits.
  3. Final Answer:

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

    Bitwise OR with 1 at pos sets bit [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 bit
  • Thinking it toggles the bit
2. Which of the following TypeScript code snippets correctly clears (sets to 0) the bit at position pos in the variable num?
easy
A. num = num & (1 << pos);
B. num = num & ~(1 << pos);
C. num = num | (1 << pos);
D. num = num ^ (1 << pos);

Solution

  1. Step 1: Understand clearing a bit

    To clear a bit, we AND the number with a mask that has 0 at the target bit and 1s elsewhere.
  2. Step 2: Create mask with NOT operator

    ~(1 << pos) creates a mask with 0 at pos and 1s elsewhere, so num & ~(1 << pos) clears the bit.
  3. Final Answer:

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

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

Solution

  1. Step 1: Understand initial values and bit positions

    Number 10 in binary is 1010. Bit positions (0-based) from right: pos 0=0, pos 1=1, pos 2=0, pos 3=1.
  2. Step 2: Apply XOR to toggle bit at position 1

    1 << 1 is 2 (binary 0010). XOR toggles bit 1: 1010 ^ 0010 = 1000 (binary), which is 8 decimal.
  3. Final Answer:

    8 -> Option A
  4. Quick Check:

    Toggle bit 1 in 10 gives 8 [OK]
Hint: XOR toggles bit; toggle 1 in 1010 gives 1000 (8) [OK]
Common Mistakes:
  • Confusing XOR with OR or AND
  • Miscounting bit positions
  • Assuming toggle sets bit always to 1
4. The following TypeScript code is intended to toggle the bit at position pos in num. What is the error?
let num = 5; // binary 0101
let pos = 2;
num = num & (1 << pos);
console.log(num);
medium
A. It only keeps the bit at pos and clears others
B. It clears the bit instead of toggling
C. It sets the bit instead of toggling
D. It causes a syntax error

Solution

  1. Step 1: Analyze the operation used

    The code uses num & (1 << pos), which ANDs num with a mask having 1 only at pos.
  2. Step 2: Understand effect of AND with mask

    This operation keeps only the bit at pos and clears all other bits, not toggling the bit.
  3. Final Answer:

    It only keeps the bit at pos and clears others -> Option A
  4. Quick Check:

    AND with mask keeps only that bit [OK]
Hint: AND with mask keeps only that bit, doesn't toggle [OK]
Common Mistakes:
  • Confusing AND with XOR for toggling
  • Expecting toggle but using AND
  • Ignoring effect on other bits
5. You have a number num = 29 (binary 11101). You want to toggle the bits at positions 0, 2, and 4 simultaneously. Which TypeScript code correctly does this?
hard
A. num = num | ((1 << 0) & (1 << 2) & (1 << 4));
B. num = num & ~((1 << 0) | (1 << 2) | (1 << 4));
C. num = num ^ ((1 << 0) | (1 << 2) | (1 << 4));
D. num = num ^ (1 << 0) & (1 << 2) & (1 << 4);

Solution

  1. Step 1: Create mask for bits 0, 2, and 4

    Using OR to combine shifted bits: (1 << 0) | (1 << 2) | (1 << 4) creates mask 10101 binary (decimal 21).
  2. Step 2: Use XOR to toggle bits

    XOR with this mask toggles bits 0, 2, and 4 in num.
  3. Final Answer:

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

    XOR with combined mask toggles multiple bits [OK]
Hint: Combine bits with OR, toggle with XOR [OK]
Common Mistakes:
  • Using AND or OR instead of XOR to toggle
  • Using AND between shifted bits instead of OR
  • Not combining bits before toggling
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