Challenge - 5 Problems
Bitwise Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bitwise AND and OR Operations
What is the output of the following C++ code snippet?
DSA C++
int a = 12; // binary: 1100 int b = 10; // binary: 1010 int c = a & b; int d = a | b; std::cout << c << " " << d << std::endl;
Attempts:
2 left
💡 Hint
Recall that & compares bits and returns 1 only if both bits are 1; | returns 1 if either bit is 1.
✗ Incorrect
12 in binary is 1100, 10 is 1010.
AND: 1100 & 1010 = 1000 (8 decimal)
OR: 1100 | 1010 = 1110 (14 decimal)
❓ Predict Output
intermediateResult of XOR and NOT Operations
What will be printed by this C++ code?
DSA C++
unsigned int x = 5; // binary: 0000 0101 unsigned int y = 9; // binary: 0000 1001 unsigned int z = x ^ y; unsigned int w = ~x; std::cout << z << " " << w << std::endl;
Attempts:
2 left
💡 Hint
XOR returns 1 if bits differ; NOT flips all bits. Remember unsigned int is 32-bit.
✗ Incorrect
5 (0000 0101) XOR 9 (0000 1001) = 0000 1100 (12 decimal).
NOT 5 flips bits: ~0000 0101 = 1111 1010 (in 32-bit unsigned, 4294967290).
❓ Predict Output
advancedLeft and Right Shift Effects on Signed Integers
What is the output of this C++ code snippet?
DSA C++
int a = -8; // binary (two's complement): 1111 1000 int b = a >> 2; int c = a << 2; std::cout << b << " " << c << std::endl;
Attempts:
2 left
💡 Hint
Right shift on signed int usually preserves sign (arithmetic shift). Left shift multiplies by powers of two.
✗ Incorrect
-8 >> 2 shifts bits right with sign extension: -8 / 4 = -2.
-8 << 2 shifts bits left: -8 * 4 = -32.
🧠 Conceptual
advancedUnderstanding Bitwise NOT on Signed Integers
What is the decimal value of ~(-1) in a 32-bit signed integer system?
Attempts:
2 left
💡 Hint
Remember that -1 in two's complement is all bits set to 1.
✗ Incorrect
-1 in 32-bit two's complement is 0xFFFFFFFF.
Applying NOT flips all bits to 0x00000000 which is 0.
❓ Predict Output
expertComplex Bitwise Expression Evaluation
What is the output of this C++ code?
DSA C++
unsigned int x = 29; // binary: 0001 1101 unsigned int y = 15; // binary: 0000 1111 unsigned int z = ((x & y) << 2) ^ (~y >> 1); std::cout << z << std::endl;
Attempts:
2 left
💡 Hint
Break down the expression step-by-step: AND, shifts, NOT, then XOR.
✗ Incorrect
x & y = 29 & 15 = 13 (0000 1101)
(x & y) << 2 = 13 << 2 = 52 (0011 0100)
~y = ~15 = 0xFFFFFFF0 (4294967280 unsigned)
~y >> 1 = 0x7FFFFFF8 (2147483640)
52 ^ 2147483640 = 2147483596
