Bird
Raised Fist0
DSA Typescriptprogramming~5 mins

Set Clear Toggle a Specific Bit in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does it mean to set a specific bit in a number?
Setting a specific bit means changing that bit to 1, regardless of its current value. It turns that bit ON.
Click to reveal answer
beginner
How do you clear a specific bit in a number?
Clearing a bit means changing that bit to 0, turning it OFF, without affecting other bits.
Click to reveal answer
beginner
What does toggle a specific bit mean?
Toggling a bit means flipping it: if it was 0, it becomes 1; if it was 1, it becomes 0.
Click to reveal answer
intermediate
In TypeScript, which operator is used to set a bit at position n?
Use the bitwise OR operator (|) with (1 << n). Example: number | (1 << n) sets bit n to 1.
Click to reveal answer
intermediate
How do you clear a bit at position n using bitwise operators in TypeScript?
Use bitwise AND (&) with the negation of (1 << n). Example: number & ~(1 << n) clears bit n.
Click to reveal answer
Which operation sets the 3rd bit (0-based) of a number to 1?
Anumber & (1 << 3)
Bnumber & ~(1 << 3)
Cnumber ^ (1 << 3)
Dnumber | (1 << 3)
How do you clear the 2nd bit of a number?
Anumber ^ (1 << 2)
Bnumber | (1 << 2)
Cnumber & ~(1 << 2)
Dnumber & (1 << 2)
What does toggling a bit do?
AFlips the bit from 0 to 1 or 1 to 0
BClears the bit to 0
CSets the bit to 1
DLeaves the bit unchanged
Which operator toggles a bit at position n?
A| (bitwise OR)
B^ (bitwise XOR)
C& (bitwise AND)
D~ (bitwise NOT)
What is the result of toggling the 1st bit of number 6 (binary 110)?
A4 (binary 100)
B7 (binary 111)
C2 (binary 010)
D6 (binary 110)
Explain how to set, clear, and toggle a specific bit in a number using bitwise operators.
Think about turning bits ON, OFF, or flipping them.
You got /3 concepts.
    Describe a real-life example where setting, clearing, or toggling bits might be useful.
    Imagine controlling lights or options with switches.
    You got /3 concepts.

      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