Challenge - 5 Problems
Shift Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of left shift operation
What is the output of this C code snippet?
C
int main() { unsigned int x = 5; // binary: 0000 0101 unsigned int y = x << 2; printf("%u", y); return 0; }
Attempts:
2 left
💡 Hint
Left shift by 2 moves bits two places to the left, multiplying by 4.
✗ Incorrect
5 in binary is 00000101. Shifting left by 2 bits gives 00010100, which is 20 in decimal.
❓ Predict Output
intermediate2:00remaining
Output of right shift operation
What is the output of this C code snippet?
C
int main() { unsigned int x = 32; // binary: 0010 0000 unsigned int y = x >> 3; printf("%u", y); return 0; }
Attempts:
2 left
💡 Hint
Right shift by 3 moves bits three places to the right, dividing by 8.
✗ Incorrect
32 in binary is 00100000. Shifting right by 3 bits gives 00000100, which is 4 in decimal.
🧠 Conceptual
advanced2:30remaining
Effect of left shift on signed integers
Consider the following code. What is the output and why?
C
int main() { int x = 1073741824; // 2^30 int y = x << 2; printf("%d", y); return 0; }
Attempts:
2 left
💡 Hint
Left shift by 2 shifts bit 30 to bit 32, which is discarded in 32-bit int.
✗ Incorrect
2^30 (0x40000000, bit 30 set) shifted left by 2 moves the bit to position 32 (discarded), resulting in a negative number due to overflow (two's complement).
🔧 Debug
advanced2:30remaining
Identify the error in right shift usage
What error will this code produce when compiled and run?
C
int main() { int x = -16; unsigned int y = x >> 2; printf("%u", y); return 0; }
Attempts:
2 left
💡 Hint
Right shifting a negative signed int keeps the sign bit, then converting to unsigned prints a large number.
✗ Incorrect
Right shifting negative signed int keeps sign bits (arithmetic shift). Assigning to unsigned int prints the large unsigned value of that bit pattern.
🚀 Application
expert3:00remaining
Calculate the number of bits set after shifts
Given the code below, what is the number of bits set to 1 in variable z after execution?
C
int main() { unsigned int x = 0b1011; // decimal 11 unsigned int y = x << 3; // shift left by 3 unsigned int z = y >> 2; // shift right by 2 int count = 0; for (int i = 0; i < 32; i++) { if ((z & (1 << i)) != 0) count++; } printf("%d", count); return 0; }
Attempts:
2 left
💡 Hint
Track the bits step by step after each shift.
✗ Incorrect
x = 0b1011 (bits set: 3). Left shift by 3: 0b1011000 (bits set: 3). Right shift by 2: 0b10110 (bits set: 3). So count is 3.