Bird
Raised Fist0
DSA Typescriptprogramming~5 mins

Set Clear Toggle a Specific Bit in DSA Typescript - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Set Clear Toggle a Specific Bit
O(1)
Understanding Time Complexity

We want to understand how long it takes to change a specific bit in a number.

How does the time needed grow when we work with bigger numbers or different bit positions?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


function toggleBit(num: number, bitPosition: number): number {
  const mask = 1 << bitPosition;
  return num ^ mask;
}
    

This code flips (toggles) the bit at the given position in the number.

Identify Repeating Operations

Look for loops or repeated steps that take time.

  • Primary operation: Bit shift and bitwise XOR.
  • How many times: Each operation runs once per function call.
How Execution Grows With Input

The operations do not repeat or grow with input size.

Input Size (n)Approx. Operations
103 (shift, XOR, return)
1003 (same steps)
10003 (same steps)

Pattern observation: The number of steps stays the same no matter the input size.

Final Time Complexity

Time Complexity: O(1)

This means the time to toggle a bit is constant and does not change with input size.

Common Mistake

[X] Wrong: "Toggling a bit takes longer if the number is bigger or the bit position is higher."

[OK] Correct: Bitwise operations work directly on fixed-size binary data, so the time stays constant regardless of number size or bit position.

Interview Connect

Knowing that bit operations run in constant time helps you write efficient code and answer questions confidently in interviews.

Self-Check

"What if we toggle multiple bits one after another? How would the time complexity change?"

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