Bird
Raised Fist0
Cprogramming~20 mins

Left shift and right shift in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shift Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A40
B10
C5
D20
Attempts:
2 left
💡 Hint
Left shift by 2 moves bits two places to the left, multiplying by 4.
❓ Predict Output
intermediate
2: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;
}
A4
B8
C16
D64
Attempts:
2 left
💡 Hint
Right shift by 3 moves bits three places to the right, dividing by 8.
🧠 Conceptual
advanced
2: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;
}
A-1073741824
B1073741824
C0
D4294967296
Attempts:
2 left
💡 Hint
Left shift by 2 shifts bit 30 to bit 32, which is discarded in 32-bit int.
🔧 Debug
advanced
2: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;
}
APrints -4
BCompilation error due to mixing signed and unsigned types
CPrints a large positive number due to sign extension and conversion
DRuntime error: undefined behavior
Attempts:
2 left
💡 Hint
Right shifting a negative signed int keeps the sign bit, then converting to unsigned prints a large number.
🚀 Application
expert
3: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;
}
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Track the bits step by step after each shift.

Practice

(1/5)
1. What does the left shift operator << do to an integer in C?
easy
A. Moves bits to the left, multiplying the number by powers of two
B. Moves bits to the right, dividing the number by powers of two
C. Reverses the bits of the number
D. Sets all bits to zero

Solution

  1. Step 1: Understand left shift operation

    The left shift operator moves all bits in the number to the left by the specified count.
  2. Step 2: Effect on the number's value

    Each left shift multiplies the number by 2, so shifting left by n multiplies by 2^n.
  3. Final Answer:

    Moves bits to the left, multiplying the number by powers of two -> Option A
  4. Quick Check:

    Left shift = multiply by 2^n [OK]
Hint: Left shift multiplies by 2 to the power of shift count [OK]
Common Mistakes:
  • Confusing left shift with right shift
  • Thinking left shift divides the number
  • Assuming left shift reverses bits
2. Which of the following is the correct syntax to shift the integer variable x right by 3 bits in C?
easy
A. x >>> 3;
B. x >> 3;
C. x << 3;
D. x >>3

Solution

  1. Step 1: Identify the right shift operator in C

    The right shift operator is >> and requires spaces or no spaces around it.
  2. Step 2: Check syntax correctness

    x >> 3; is correct syntax. x << 3; is left shift, x >>> 3; is invalid in C, and x >>3 is missing a semicolon.
  3. Final Answer:

    x >> 3; -> Option B
  4. Quick Check:

    Right shift syntax = x >> n; [OK]
Hint: Right shift uses >> with semicolon to end statement [OK]
Common Mistakes:
  • Using >>> which is not valid in C
  • Confusing left shift << with right shift >>
  • Omitting semicolon at end
3. What is the output of the following C code?
int x = 4;
int y = x << 2;
printf("%d", y);
medium
A. 0
B. 1
C. 8
D. 16

Solution

  1. Step 1: Understand the initial value of x

    x is 4, which in binary is 00000100.
  2. Step 2: Apply left shift by 2 bits

    Shifting left by 2 bits multiplies 4 by 2^2 = 4 * 4 = 16.
  3. Final Answer:

    16 -> Option D
  4. Quick Check:

    4 << 2 = 16 [OK]
Hint: Left shift by n multiplies by 2^n [OK]
Common Mistakes:
  • Calculating 4 << 2 as 8 instead of 16
  • Confusing left shift with right shift
  • Ignoring the shift count
4. Find the error in this C code snippet:
int a = 32;
int b = a >> 2
printf("%d", b);
medium
A. Wrong operator used for right shift
B. Incorrect printf format specifier
C. Missing semicolon after right shift operation
D. Variable b is not declared

Solution

  1. Step 1: Check syntax line by line

    The line int b = a >> 2 is missing a semicolon at the end.
  2. Step 2: Verify other parts

    The operator >> is correct, printf uses %d for int, and b is declared properly.
  3. Final Answer:

    Missing semicolon after right shift operation -> Option C
  4. Quick Check:

    Every statement needs semicolon [OK]
Hint: Check for missing semicolons after shift operations [OK]
Common Mistakes:
  • Assuming >> is invalid operator
  • Thinking printf format is wrong
  • Ignoring missing semicolon errors
5. Given int x = 1;, what is the value of y after this operation?
int y = (x << 3) >> 1;
hard
A. 4
B. 8
C. 16
D. 2

Solution

  1. Step 1: Calculate left shift (x << 3)

    Left shifting 1 by 3 bits multiplies 1 by 2^3 = 8.
  2. Step 2: Calculate right shift of result by 1

    Right shifting 8 by 1 bit divides 8 by 2^1 = 2, so 8 / 2 = 4.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    (1 << 3) >> 1 = 8 >> 1 = 4 [OK]
Hint: Left shift then right shift: multiply then divide by powers of two [OK]
Common Mistakes:
  • Calculating (1 << 3) as 16
  • Ignoring right shift effect
  • Mixing up shift directions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard