Challenge - 5 Problems
Bitwise Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What 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-based from right)?
DSA Python
num = 8 bit_position = 3 result = num | (1 << bit_position) print(result)
Attempts:
2 left
💡 Hint
Remember that setting a bit means making sure it is 1, using OR operation.
✗ Incorrect
Number 8 in binary is 1000. The 3rd bit (0-based) is already set (1). Setting it again keeps the number unchanged, so output is 8.
❓ Predict Output
intermediate2:00remaining
What 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-based from right)?
DSA Python
num = 7 bit_position = 1 result = num & ~(1 << bit_position) print(result)
Attempts:
2 left
💡 Hint
Clearing a bit means making it 0 using AND with NOT mask.
✗ Incorrect
Number 7 is 0111. Clearing bit 1 (second from right) changes it to 0101 which is 5.
❓ Predict Output
advanced2:00remaining
What 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-based from right)?
DSA Python
num = 10 bit_position = 2 result = num ^ (1 << bit_position) print(result)
Attempts:
2 left
💡 Hint
Toggling flips the bit: 0 to 1 or 1 to 0 using XOR.
✗ Incorrect
Number 10 is 1010. Toggling bit 2 (which is 0) flips it to 1, resulting in 1110 which is 14.
🧠 Conceptual
advanced2:00remaining
Which operation correctly clears the 0th bit of a number n?
Choose the correct expression to clear the 0th bit (least significant bit) of a number n.
Attempts:
2 left
💡 Hint
Clearing a bit uses AND with NOT mask.
✗ Incorrect
To clear a bit, AND with the complement of a mask that has 1 at that bit position.
🔧 Debug
expert2:00remaining
What error does this code raise when toggling bit 5 of number 15?
Analyze the code below and select the error it raises, if any.
DSA Python
num = 15 bit_position = 5 result = num ^ (1 >> bit_position) print(result)
Attempts:
2 left
💡 Hint
Check the bit shift direction and its effect.
✗ Incorrect
Right shifting 1 by 5 bits results in 0, so XOR with 0 leaves number unchanged, output 15.