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 3rd bit of number 8?
Given the number 8 (binary 1000), what is the output after setting the 3rd bit (0-indexed from right)?
DSA Javascript
let num = 8; let bitPosition = 3; let result = num | (1 << bitPosition); console.log(result);
Attempts:
2 left
💡 Hint
Remember that bits are counted from 0 starting at the rightmost bit.
✗ Incorrect
The number 8 in binary is 1000. Setting the 3rd bit (0-indexed) means setting the bit that is already set, so the number remains 8.
❓ Predict Output
intermediateWhat is the output after clearing the 1st bit of number 7?
Given the number 7 (binary 0111), what is the output after clearing the 1st bit (0-indexed from right)?
DSA Javascript
let num = 7; let bitPosition = 1; let result = num & ~(1 << bitPosition); console.log(result);
Attempts:
2 left
💡 Hint
Clearing a bit means setting it to 0 using AND with the negated mask.
✗ Incorrect
Number 7 is 0111 in binary. Clearing the 1st bit (second from right) changes it to 0101, which is 5.
❓ Predict Output
advancedWhat is the output after toggling the 2nd bit of number 10?
Given the number 10 (binary 1010), what is the output after toggling the 2nd bit (0-indexed from right)?
DSA Javascript
let num = 10; let bitPosition = 2; let result = num ^ (1 << bitPosition); console.log(result);
Attempts:
2 left
💡 Hint
Toggling flips the bit: 0 becomes 1, 1 becomes 0.
✗ Incorrect
Number 10 is 1010 in binary. Toggling the 2nd bit flips it from 0 to 1, resulting in 1110 which is 14.
❓ Predict Output
advancedWhat is the output after toggling the 0th bit of number 15 twice?
Given the number 15 (binary 1111), what is the output after toggling the 0th bit twice in a row?
DSA Javascript
let num = 15; let bitPosition = 0; num = num ^ (1 << bitPosition); num = num ^ (1 << bitPosition); console.log(num);
Attempts:
2 left
💡 Hint
Toggling the same bit twice returns it to the original state.
✗ Incorrect
Toggling the 0th bit of 1111 flips it to 1110 (14), toggling again flips it back to 1111 (15).
🧠 Conceptual
expertHow many bits are set to 1 after setting, clearing, and toggling bits on number 21?
Start with number 21 (binary 10101). Perform these operations in order:
1. Set the 1st bit
2. Clear the 4th bit
3. Toggle the 2nd bit
How many bits are set to 1 in the final number?
1. Set the 1st bit
2. Clear the 4th bit
3. Toggle the 2nd bit
How many bits are set to 1 in the final number?
DSA Javascript
let num = 21; num = num | (1 << 1); // set 1st bit num = num & ~(1 << 4); // clear 4th bit num = num ^ (1 << 2); // toggle 2nd bit // Count bits set to 1 let count = 0; for(let i = 0; i < 8; i++) { if(num & (1 << i)) count++; } console.log(count);
Attempts:
2 left
💡 Hint
Track each operation step-by-step in binary.
✗ Incorrect
Start: 21 = 10101 (bits 4,2,0 set)
Set 1st bit: 10101 | 00010 = 10111 (bits 4,2,1,0 set)
Clear 4th bit: 10111 & 01111 = 00111 (bits 2,1,0 set)
Toggle 2nd bit: 00111 ^ 00100 = 00011 (bits 1,0 set)
Count bits set: 2 bits.
