0
0
Cprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Left shift and right shift
Start with number x
Apply left shift x << n
Multiply x by 2^n
Apply right shift x >> n
Divide x by 2^n (floor)
End with new value
Start with a number, shift bits left to multiply by powers of two, shift bits right to divide by powers of two, then get the new value.
Execution Sample
C
int x = 4;
int left = x << 1;
int right = x >> 1;
printf("%d %d", left, right);
This code shifts 4 left by 1 (multiply by 2) and right by 1 (divide by 2), then prints the results.
Execution Table
StepOperationValue BeforeShift AmountValue AfterExplanation
1Initialize xN/AN/A4Start with x = 4
2Left shift x << 14 (binary 0100)18 (binary 1000)Shifts bits left by 1, multiplies by 2
3Right shift x >> 14 (binary 0100)12 (binary 0010)Shifts bits right by 1, divides by 2 (floor)
4Print resultsN/AN/A8 2Outputs left and right shift results
💡 All operations done, program ends after printing.
Variable Tracker
VariableStartAfter Left ShiftAfter Right ShiftFinal
x4444
leftN/A888
rightN/AN/A22
Key Moments - 3 Insights
Why does left shifting by 1 multiply the number by 2?
Because shifting bits left moves all bits one place higher, which doubles the value (see execution_table step 2).
Why does right shifting by 1 divide the number by 2 and round down?
Because shifting bits right moves bits one place lower, effectively dividing by 2 and dropping any remainder (see execution_table step 3).
Does the original variable x change after shifting?
No, shifting creates new values stored in separate variables (left and right), x remains unchanged (see variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'left' after step 2?
A4
B8
C2
D16
💡 Hint
Check the 'Value After' column in step 2 of execution_table.
At which step does the value become 2 after shifting?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'Value After' column where value is 2 in execution_table.
If we change x to 8, what would be the value of 'right' after right shift by 1?
A16
B8
C4
D2
💡 Hint
Right shift divides by 2, so half of 8 is 4 (see concept_flow and execution_table logic).
Concept Snapshot
Left shift (x << n) moves bits left n times, multiplying x by 2^n.
Right shift (x >> n) moves bits right n times, dividing x by 2^n (floor).
Original variable stays unchanged unless assigned.
Useful for fast multiplication/division by powers of two.
Shifts work on binary representation of numbers.
Full Transcript
This lesson shows how left and right bit shifts work in C. We start with a number x. Left shifting x by 1 moves bits left, doubling the number. Right shifting x by 1 moves bits right, halving the number and rounding down. The code example uses x=4, shifts left and right by 1, and prints results 8 and 2. Variables left and right hold shifted values; x stays 4. The execution table traces each step with values and explanations. Key points clarify why shifting multiplies or divides by powers of two and that original variables don't change unless reassigned. The quiz tests understanding of values after shifts and effects of changing x.