Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the bit at position 2 in the number.
DSA Typescript
let num = 8; // binary 1000 num = num [1] (1 << 2); console.log(num);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' which clears bits instead of setting them.
Using '>>' which shifts bits instead of setting.
✗ Incorrect
Using the bitwise OR operator '|' sets the bit at position 2 without changing other bits.
2fill in blank
mediumComplete the code to clear the bit at position 1 in the number.
DSA Typescript
let num = 7; // binary 0111 num = num [1] ~(1 << 1); console.log(num);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' which sets bits instead of clearing.
Using '^' which toggles bits instead of clearing.
✗ Incorrect
Using '&' with the negated mask clears the bit at position 1.
3fill in blank
hardFix the error in the code to toggle the bit at position 3.
DSA Typescript
let num = 10; // binary 1010 num = num [1] (1 << 3); console.log(num);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '|' which only sets bits.
Using '&' which only clears bits.
✗ Incorrect
The XOR '^' operator toggles the bit at the given position.
4fill in blank
hardFill both blanks to set bit 0 and clear bit 2 in the number.
DSA Typescript
let num = 5; // binary 0101 num = (num [1] (1 << 0)) [2] ~(1 << 2); console.log(num);
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' to set bits.
Using '|' to clear bits.
✗ Incorrect
First set bit 0 using '|', then clear bit 2 using '&' with negated mask.
5fill in blank
hardFill all three blanks to toggle bit 1, set bit 3, and clear bit 0 in the number.
DSA Typescript
let num = 9; // binary 1001 num = (num [1] (1 << 1)) [2] (1 << 3) [3] ~(1 << 0); console.log(num);
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators for set, clear, and toggle.
Incorrect order causing wrong final bits.
✗ Incorrect
Toggle bit 1 with '^', set bit 3 with '|', clear bit 0 with '&' and negation.
