Dart int operator-left-shift
Syntax & Examples
int.operator-left-shift operator
The `<<` operator in Dart shifts the bits of this integer to the left by the specified amount.
Syntax of int.operator-left-shift
The syntax of int.operator-left-shift operator is:
operator <<(int shiftAmount) → intThis operator-left-shift operator of int shift the bits of this integer to the left by shiftAmount.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
shiftAmount | required | The number of positions to shift the bits to the left. |
✐ Examples
1 Left shifting by 2 positions
In this example,
- We assign the value
5to the integer variablenum. - We left shift
numby 2 positions using the<<operator. - We print the result to standard output.
Dart Program
void main() {
int num = 5;
int shiftedNum = num << 2;
print('Result of left shifting $num by 2 positions: $shiftedNum');
}Output
Result of left shifting 5 by 2 positions: 20
2 Left shifting by 3 positions
In this example,
- We assign the value
10to the integer variablenum. - We left shift
numby 3 positions using the<<operator. - We print the result to standard output.
Dart Program
void main() {
int num = 10;
int shiftedNum = num << 3;
print('Result of left shifting $num by 3 positions: $shiftedNum');
}Output
Result of left shifting 10 by 3 positions: 80
3 Left shifting by 1 position
In this example,
- We assign the value
3to the integer variablenum. - We left shift
numby 1 position using the<<operator. - We print the result to standard output.
Dart Program
void main() {
int num = 3;
int shiftedNum = num << 1;
print('Result of left shifting $num by 1 position: $shiftedNum');
}Output
Result of left shifting 3 by 1 position: 6
Summary
In this Dart tutorial, we learned about operator-left-shift operator of int: the syntax and few working examples with output and detailed explanation for each example.