Challenge - 5 Problems
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after setting the 2nd bit of 8?
Given the number 8 (binary 1000), what is the output after setting the 2nd bit (0-based from right)?
DSA Typescript
let num = 8; let bitPosition = 2; num = num | (1 << bitPosition); console.log(num);
Attempts:
2 left
💡 Hint
Remember, setting a bit means making sure that bit is 1 using OR operation.
✗ Incorrect
8 in binary is 1000. Setting the 2nd bit (counting from 0) means setting the bit for 4 (2^2). 1000 OR 0100 = 1100 which is 12 in decimal.
❓ Predict Output
intermediateWhat is the output after clearing the 3rd bit of 15?
Given the number 15 (binary 1111), what is the output after clearing the 3rd bit (0-based from right)?
DSA Typescript
let num = 15; let bitPosition = 3; num = num & ~(1 << bitPosition); console.log(num);
Attempts:
2 left
💡 Hint
Clearing a bit means making that bit 0 using AND with NOT mask.
✗ Incorrect
15 in binary is 1111. Clearing the 3rd bit (bit for 8) means 1111 AND 0111 = 0111 which is 7 in decimal.
❓ Predict Output
advancedWhat is the output after toggling the 1st bit of 10?
Given the number 10 (binary 1010), what is the output after toggling the 1st bit (0-based from right)?
DSA Typescript
let num = 10; let bitPosition = 1; num = num ^ (1 << bitPosition); console.log(num);
Attempts:
2 left
💡 Hint
Toggling flips the bit: 0 to 1 or 1 to 0 using XOR.
✗ Incorrect
10 in binary is 1010. Toggling the 1st bit (bit for 2) flips it from 1 to 0: 1010 XOR 0010 = 1000 which is 8 in decimal.
❓ Predict Output
advancedWhat is the output after toggling the 0th bit of 7?
Given the number 7 (binary 0111), what is the output after toggling the 0th bit (0-based from right)?
DSA Typescript
let num = 7; let bitPosition = 0; num = num ^ (1 << bitPosition); console.log(num);
Attempts:
2 left
💡 Hint
Toggle flips the bit at the given position.
✗ Incorrect
7 in binary is 0111. Toggling the 0th bit flips it from 1 to 0: 0111 XOR 0001 = 0110 which is 6 in decimal.
🧠 Conceptual
expertWhich operation correctly clears the 4th bit of a number n?
You want to clear (set to 0) the 4th bit (0-based from right) of a number n. Which of the following expressions correctly does this?
Attempts:
2 left
💡 Hint
Clearing a bit uses AND with the NOT of the bit mask.
✗ Incorrect
To clear a bit, you AND the number with the inverse of the bit mask. Option D does this correctly.
Option D sets the bit, C toggles it, and D is incorrect logic.
