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?
Given the initial integer value 8 (binary 1000), what is the output after setting the 3rd bit (0-based from right)?
DSA C
int x = 8; // binary 1000 int bit = 3; x = x | (1 << bit); printf("%d\n", x);
Attempts:
2 left
💡 Hint
Remember that bits are counted from 0 starting at the rightmost bit.
✗ Incorrect
Initial value 8 (binary 1000). Bit positions (0-based from right): bit 0=0, bit 1=0, bit 2=0, bit 3=1. Setting bit 3: 1 << 3 = 8 (binary 1000). 1000 | 1000 = 1000 (decimal 8).
❓ Predict Output
intermediate2:00remaining
What is the output after clearing the 2nd bit?
Given the integer value 15 (binary 1111), what is the output after clearing the 2nd bit (0-based from right)?
DSA C
int x = 15; // binary 1111 int bit = 2; x = x & ~(1 << bit); printf("%d\n", x);
Attempts:
2 left
💡 Hint
Clearing a bit means setting it to 0 using AND with the negation of the bit mask.
✗ Incorrect
Clearing bit 2 means turning off the bit at position 2. 1 << 2 is 4 (binary 0100). Negation is 1011. 1111 & 1011 = 1011 (decimal 11).
❓ Predict Output
advanced2:00remaining
What is the output after toggling the 1st bit?
Given the integer value 10 (binary 1010), what is the output after toggling the 1st bit (0-based from right)?
DSA C
int x = 10; // binary 1010 int bit = 1; x = x ^ (1 << bit); printf("%d\n", x);
Attempts:
2 left
💡 Hint
Toggling a bit flips it: if it was 1, it becomes 0; if 0, it becomes 1.
✗ Incorrect
Toggling bit 1 means XOR with 1 << 1 (2). 10 (1010) XOR 2 (0010) = 8 (1000).
🧠 Conceptual
advanced2:00remaining
Which operation clears a specific bit in an integer?
You want to clear (set to 0) the bit at position k in an integer x. Which of the following operations correctly does this?
Attempts:
2 left
💡 Hint
Clearing a bit means making sure that bit is 0 while keeping others unchanged.
✗ Incorrect
Using AND with the negation of the bit mask clears the bit at position k.
🔧 Debug
expert2:00remaining
Why does this code fail to toggle the 4th bit?
Consider this code snippet intended to toggle the 4th bit of x:
int x = 20;
int bit = 4;
x = x ^ 1 << bit;
printf("%d\n", x);
Why might this code not work as expected?
DSA C
int x = 20; int bit = 4; x = x ^ 1 << bit; printf("%d\n", x);
Attempts:
2 left
💡 Hint
Check operator precedence between ^ and << in C.
✗ Incorrect
In C, << has higher precedence than ^, so x ^ 1 << bit is evaluated as (x ^ 1) << bit, which is not intended. Parentheses are needed: x ^ (1 << bit).
