C Left Shift Assignment Operator
In C, the Left Shift Assignment <<= operator is used to shift the bits of a variable to the left by a specified number of positions and assign the result back to the variable.
Left Shift Assignment operation effectively multiplies the variable by 2^n, where n is the number of positions to shift. It is commonly used for optimizing multiplication by powers of two.
Syntax of the Left Shift Assignment Operator
The syntax to use the Left Shift Assignment operator is:
variable <<= shift_count;
Explanation:
variable: The variable whose bits are to be shifted.<<=: The left shift assignment operator.shift_count: The number of positions to shift the bits to the left.- Return Value: The left-shifted value is assigned back to
variable.
Examples of the Left Shift Assignment Operator
1. Basic Left Shift Assignment
In this example, we will use the <<= operator to shift the bits of a number two positions to the left.
main.c
#include <stdio.h>
int main() {
int num = 5;
num <<= 2; // Left shift by 2 positions
printf("Result after left shift: %d\n", num);
return 0;
}
Explanation:
- We initialize an integer variable
numwith a value of 5. - The statement
num <<= 2shifts the bits ofnumtwo positions to the left. - Since a left shift by one position multiplies the number by 2, shifting by two positions multiplies it by
2^2 = 4. - The new value of
numis5 × 4 = 20, which is printed as output.
Output:
Result after left shift: 20
2. Left Shift Assignment in a Loop
In this example, we will apply the <<= operator inside a for loop to demonstrate progressive left shifts.
main.c
#include <stdio.h>
int main() {
int num = 1;
// Left shift the value progressively
for (int i = 0; i < 4; i++) {
num <<= 1;
printf("After shift %d: %d\n", i + 1, num);
}
return 0;
}
Explanation:
- We initialize an integer
numwith a value of 1. - A
forloop runs four times, shiftingnumby one position in each iteration. - Each left shift doubles the previous value of
num. - The output shows how the value changes after each shift.
Output:
After shift 1: 2
After shift 2: 4
After shift 3: 8
After shift 4: 16
3. Left Shift Assignment with Negative Numbers
In this example, we will observe the behavior of the <<= operator when used with negative numbers.
main.c
#include <stdio.h>
int main() {
int num = -3;
num <<= 1; // Left shift by 1 position
printf("Result after left shift: %d\n", num);
return 0;
}
Explanation:
- We initialize an integer
numwith a value of -3. - The statement
num <<= 1shifts the bits ofnumone position to the left. - Since negative numbers are stored in two’s complement form, the behavior may be compiler-dependent.
- Some systems might preserve the sign, while others may lead to unexpected results.
Output:
Result after left shift: -6
Conclusion
In this tutorial, we covered the Left Shift Assignment <<= operator in C:
- The operator shifts bits to the left and assigns the new value to the variable.
- Each left shift by
npositions multiplies the value by2^n. - It is commonly used for efficient multiplication by powers of two.
- When used with negative numbers, behavior can vary based on the compiler.
