Dart int operator-right-shift
Syntax & Examples


int.operator-right-shift operator

The `>>` operator in Dart shifts the bits of this integer to the right by a specified amount.


Syntax of int.operator-right-shift

The syntax of int.operator-right-shift operator is:

operator >>(int shiftAmount) → int

This operator-right-shift operator of int shift the bits of this integer to the right by shiftAmount.

Parameters

ParameterOptional/RequiredDescription
shiftAmountrequiredThe number of positions to shift the bits to the right.


✐ Examples

1 Bit-wise right shift

In this example,

  1. We create an integer variable num1 with the value 8 and a shift amount of 2.
  2. We use the `>>` operator to perform the bit-wise right shift operation on num1.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num1 = 8;
  int shiftAmount1 = 2;
  int result1 = num1 >> shiftAmount1;
  print('Right shift of $num1 by $shiftAmount1 positions: $result1');
}

Output

Right shift of 8 by 2 positions: 2

2 Bit-wise right shift

In this example,

  1. We create an integer variable num2 with the value 16 and a shift amount of 3.
  2. We use the `>>` operator to perform the bit-wise right shift operation on num2.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num2 = 16;
  int shiftAmount2 = 3;
  int result2 = num2 >> shiftAmount2;
  print('Right shift of $num2 by $shiftAmount2 positions: $result2');
}

Output

Right shift of 16 by 3 positions: 2

3 Bit-wise right shift

In this example,

  1. We create an integer variable num3 with the value 32 and a shift amount of 1.
  2. We use the `>>` operator to perform the bit-wise right shift operation on num3.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num3 = 32;
  int shiftAmount3 = 1;
  int result3 = num3 >> shiftAmount3;
  print('Right shift of $num3 by $shiftAmount3 positions: $result3');
}

Output

Right shift of 32 by 1 positions: 16

Summary

In this Dart tutorial, we learned about operator-right-shift operator of int: the syntax and few working examples with output and detailed explanation for each example.