0
0
Embedded Cprogramming~10 mins

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

Choose your learning style9 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.