Bird
Raised Fist0
Embedded Cprogramming~10 mins

Left shift and right shift behavior in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Left shift and right shift behavior
Start with number
↓
Apply left shift <<
↓
Bits move left, zeros fill right
↓
Result is number * 2^shift_count
↓
Apply right shift >>
↓
Bits move right, fill depends on sign
↓
Result is number / 2^shift_count (floor)
↓
End
Left shift moves bits left, multiplying by powers of two; right shift moves bits right, dividing by powers of two, with fill depending on sign.
Execution Sample
Embedded C
int x = 4;  // binary 00000100
int y = x << 1; // left shift by 1
int z = x >> 1; // right shift by 1
This code shifts bits of x left and right by 1, showing multiplication and division by 2.
Execution Table
StepVariableValue (decimal)Value (binary 8-bit)OperationResult Explanation
1x400000100Initial valueStarting number in binary
2y800001000x << 1Bits shifted left by 1, multiply by 2
3z200000010x >> 1Bits shifted right by 1, divide by 2
4x-811111000x = -8 (two's complement)Negative number binary representation
5y-1611110000x << 1Left shift negative number, bits shifted left
6z-411111100x >> 1Right shift negative number, sign bit fills left
💡 All shifts done; left shift multiplies by 2, right shift divides by 2 with sign extension for negatives.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6
x444-8-8-8
yundefined888-16-16
zundefinedundefined222-4
Key Moments - 3 Insights
Why does right shifting a negative number fill with 1s on the left?
Because in embedded C, right shift of signed negative numbers usually performs arithmetic shift, preserving the sign by filling left bits with 1s (see step 6 in execution_table).
Does left shifting always multiply the number by 2?
Left shift multiplies by 2 only if no bits are lost (no overflow). For example, shifting -8 left by 1 gives -16 (step 5), but if bits shift out, the value changes unexpectedly.
What happens to bits shifted out of the variable?
Bits shifted out are lost and do not wrap around; this can cause data loss or change in sign (see step 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the binary value of z?
A00001000
B00000100
C00000010
D11111100
💡 Hint
Check the 'Value (binary 8-bit)' column for step 3 in execution_table.
At which step does the variable x become negative?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Value (decimal)' column for x in execution_table rows.
If we right shift x = -8 by 1, what is the expected decimal result according to the table?
A-4
B-2
C4
D8
💡 Hint
See step 6 in execution_table for x >> 1 result.
Concept Snapshot
Left shift (<<) moves bits left, filling right with zeros, multiplying by 2 per shift.
Right shift (>>) moves bits right, filling left with sign bit for signed numbers, dividing by 2 per shift.
Shifting can cause data loss if bits shift out.
Right shift of negative numbers usually keeps sign (arithmetic shift).
Use shifts carefully to avoid overflow or unexpected sign changes.
Full Transcript
This lesson shows how left and right bit shifts work in embedded C. Starting with a number, left shift moves bits left, adding zeros on the right, effectively multiplying the number by powers of two. Right shift moves bits right, filling the left side depending on the sign: zeros for positive, ones for negative numbers, effectively dividing by powers of two. The execution table traces values step-by-step, including positive and negative examples. Key points include how negative numbers keep their sign when right shifted and how bits shifted out are lost. The quiz tests understanding of binary values and sign behavior during shifts.

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