Using the triple-shift (>>>) operator in Dart

439

You can refer to my related answer here.


Any language features that are introduced in a given version require that version as the minimum constraint for your project. This means you need to update your pubspec.yaml:

environment:
  sdk: '>=2.14.0 <3.0.0'

Now, you can use:

var foo = 42;
foo >>> 2;
Share:
439
Suragch
Author by

Suragch

Read my story here: Programming was my god

Updated on January 01, 2023

Comments

  • Suragch
    Suragch over 1 year

    Supposedly Dart 2.14 included the triple shift operator (>>>), but when I try to use it I get an error:

    print(0xff >>> 1);
    

    The compiler highlights the last > of the three and says:

    Expected an identifier.

    This is true on my local machine and in DartPad. Both are using the version of Dart 2.14 shipped with Flutter 2.5.

    Am I doing something wrong or is this a problem with the release?

    • Mangaldeep Pannu
      Mangaldeep Pannu over 2 years
      Referring to docs api.dart.dev/stable/2.14.1/dart-core/int/… it says not implemented yet.
    • jamesdlin
      jamesdlin over 2 years
      Did you set 2.14 as the minimum Dart SDK version in pubspec.yaml?
    • Suragch
      Suragch over 2 years
      @jamesdlin, you're right. I forgot.
  • Suragch
    Suragch over 2 years
    Oops, I should have known that. Thank you. Any idea why it doesn't work in DartPad?
  • Suragch
    Suragch over 2 years
    Since DartPad uses 2.14.0, that's why it doesn't work there?
  • jamesdlin
    jamesdlin over 2 years
    @Suragch The null-safety toggle suggests that DartPad is capable of running at different compatibility levels. Even though it's "Based on ... Dart SDK 2.14.0" probably doesn't mean that it's using 2.14.0 as the minimum SDK version. It's probably a bug that should be reported.
  • Subarata Talukder
    Subarata Talukder over 2 years
    @Suragch: Although Dart 2.14 added the unsigned shift operator (>>>) and DartPad also uses 2.14 But why this code not working in DartPad is quite unknown. Click to details: dart.dev/guides/language/evolution#dart-214
  • Suragch
    Suragch over 2 years
    Update: >>> works in DartPad now.