Bird
Raised Fist0
Embedded Cprogramming~5 mins

Left shift and right shift behavior in Embedded C - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the left shift operator (<<) do in C?
The left shift operator (<<) moves the bits of a number to the left by a specified number of positions, filling the right side with zeros. It effectively multiplies the number by 2 for each shift position.
Click to reveal answer
beginner
What happens to bits shifted out on the left side during a left shift?
Bits shifted out on the left side are discarded and lost. They do not wrap around or reappear on the right side.
Click to reveal answer
intermediate
How does the right shift operator (>>) behave for unsigned integers in C?
For unsigned integers, the right shift operator (>>) shifts bits to the right and fills the left side with zeros. This is called a logical right shift.
Click to reveal answer
intermediate
How does the right shift operator (>>) behave for signed integers in C?
For signed integers, the right shift operator (>>) usually performs an arithmetic right shift, which fills the left side with the sign bit (0 for positive, 1 for negative) to preserve the number's sign.
Click to reveal answer
advanced
What is a potential risk when shifting bits more than the width of the data type?
Shifting bits by a number equal to or greater than the width of the data type (e.g., 32 for a 32-bit int) leads to undefined behavior in C, which can cause unpredictable results.
Click to reveal answer
What does the expression (1 << 3) evaluate to in C?
A1
B3
C8
D0
In C, what happens when you right shift an unsigned int by 1?
ABits shift right, leftmost bit filled with 0
BBits shift right, leftmost bit filled with sign bit
CBits shift left, rightmost bit filled with 0
DUndefined behavior
What is the result of right shifting a negative signed integer in C?
AUndefined behavior
BLeft bits filled with 0
CBits rotate around
DLeft bits filled with 1 (sign bit), preserving sign
What happens if you shift an int by 32 bits on a 32-bit system?
AResult is always zero
BUndefined behavior
CBits rotate
DCompiler error
Which operator is used for left bit shifting in C?
A<<
B>>
C&
D|
Explain how left shift and right shift operators work in C for both signed and unsigned integers.
Think about how bits move and what fills the empty spaces.
You got /5 concepts.
    What are the risks of shifting bits incorrectly in embedded C programming?
    Consider what happens when shifting beyond data size or shifting signed values.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the left shift operator << do to an unsigned integer in embedded C?
      easy
      A. Divides the number by powers of two
      B. Inverts all bits of the number
      C. Multiplies the number by powers of two
      D. Leaves the number unchanged

      Solution

      1. Step 1: Understand left shift operation

        The left shift operator moves bits to the left, adding zeros on the right.
      2. Step 2: Effect on unsigned integer value

        Shifting left by 1 bit multiplies the number by 2, shifting by n bits multiplies by 2^n.
      3. Final Answer:

        Multiplies the number by powers of two -> Option C
      4. Quick Check:

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

      Solution

      1. Step 1: Identify right shift operator syntax

        In embedded C, right shift is done using >> operator with spaces optional.
      2. Step 2: Check options for correct syntax

        The correct syntax is x >> 3; using the right shift operator >>, shift amount 3, and semicolon to form a valid statement. Lacking semicolon, using left shift <<, or invalid operator like >>> are incorrect.
      3. Final Answer:

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

        Right shift syntax = x >> n; [OK]
      Hint: Right shift uses >> operator with semicolon [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 code snippet?
      unsigned int x = 4;
      unsigned int y = x << 2;
      printf("%u", y);
      medium
      A. 16
      B. 8
      C. 1
      D. 32

      Solution

      1. Step 1: Understand initial value and shift

        Variable x is 4 (binary 100). Left shifting by 2 bits moves bits left twice.
      2. Step 2: Calculate shifted value

        4 << 2 = 4 * 2^2 = 4 * 4 = 16.
      3. Final Answer:

        16 -> Option A
      4. Quick Check:

        4 << 2 = 16 [OK]
      Hint: Left shift by n multiplies by 2^n [OK]
      Common Mistakes:
      • Confusing left shift with right shift
      • Calculating 4 << 2 as 8 instead of 16
      • Ignoring unsigned type behavior
      4. Consider this code snippet:
      int x = -16;
      int y = x >> 2;
      printf("%d", y);

      What is the likely output assuming arithmetic right shift?
      medium
      A. -4
      B. 4
      C. -1
      D. 0

      Solution

      1. Step 1: Understand signed right shift behavior

        For signed integers, right shift usually preserves the sign bit (arithmetic shift).
      2. Step 2: Calculate shifted value

        -16 in binary (two's complement) is shifted right by 2 bits, dividing by 4 but keeping sign, so -16 >> 2 = -4.
      3. Final Answer:

        -4 -> Option A
      4. Quick Check:

        Signed right shift divides and keeps sign [OK]
      Hint: Signed right shift divides and keeps sign bit [OK]
      Common Mistakes:
      • Assuming right shift always fills with zeros
      • Confusing signed and unsigned shift results
      • Expecting positive result from negative input
      5. You want to multiply an unsigned 8-bit integer val by 8 using bit shifts without overflow. Which code snippet is correct?
      unsigned char val = 20;
      unsigned char result = val << 3;

      What should you check before shifting to avoid overflow?
      hard
      A. Ensure val <= 8 before shifting
      B. Ensure val <= 31 before shifting
      C. Ensure val <= 20 before shifting
      D. No check needed, shifting never overflows

      Solution

      1. Step 1: Understand shift and overflow risk

        Shifting left by 3 multiplies by 8. For 8-bit unsigned, max value is 255.
      2. Step 2: Calculate max safe value

        To avoid overflow: val * 8 <= 255 -> val <= 31.875, so val must be ≤ 31.
      3. Final Answer:

        Ensure val <= 31 before shifting -> Option B
      4. Quick Check:

        Max val before shift = 255 / 8 = 31 [OK]
      Hint: Check max value before shift to prevent overflow [OK]
      Common Mistakes:
      • Ignoring overflow risk on left shift
      • Assuming shifting never causes overflow
      • Using wrong max value for 8-bit unsigned
      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