Bird
Raised Fist0
DSA Typescriptprogramming~15 mins

Set Clear Toggle a Specific Bit in DSA Typescript - Build from Scratch

Choose your learning style10 modes available
Set Clear Toggle a Specific Bit
📖 Scenario: Imagine you are working with a simple control panel that uses bits to turn features on or off. Each bit in a number represents a feature: 1 means ON, 0 means OFF.You want to learn how to change these bits one by one: set a bit to 1, clear a bit to 0, or toggle a bit from 0 to 1 or 1 to 0.
🎯 Goal: You will write TypeScript code to set, clear, and toggle a specific bit in a number. You will practice using bitwise operations to control individual bits.
📋 What You'll Learn
Create a variable called number with the value 42.
Create a variable called bitPosition with the value 3.
Create three variables called setBit, clearBit, and toggleBit that store the results of setting, clearing, and toggling the bit at bitPosition in number.
Print the values of setBit, clearBit, and toggleBit.
💡 Why This Matters
🌍 Real World
Bitwise operations are used in hardware control, game development, and performance-critical applications to manage flags and settings efficiently.
💼 Career
Understanding bitwise manipulation is important for embedded systems developers, game programmers, and anyone working close to the hardware or optimizing code.
Progress0 / 4 steps
1
Create the initial number
Create a variable called number and set it to 42.
DSA Typescript
Hint

Use const number = 42; to create the variable.

2
Create the bit position variable
Create a variable called bitPosition and set it to 3.
DSA Typescript
Hint

Use const bitPosition = 3; to create the variable.

3
Set, clear, and toggle the bit
Create three variables called setBit, clearBit, and toggleBit. Use bitwise operations to set, clear, and toggle the bit at bitPosition in number.
DSA Typescript
Hint

Use | to set, & ~ to clear, and ^ to toggle the bit.

4
Print the results
Print the values of setBit, clearBit, and toggleBit using three separate console.log statements.
DSA Typescript
Hint

Use console.log(setBit);, console.log(clearBit);, and console.log(toggleBit); to print the 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