5

The current version of Dart Editor is showing the bitwise XOR operator as not defined for class bool

I don't see it defined in num.dart either.

Ex:

bool x = a ^ b;

The editor shows the "Caret" as not defined.

Update: Dart's api spec only allows bitwise XOR on integers. I fixed my code to properly work with bools.

Florian Loitsch
  • 7,698
  • 25
  • 30
EndlessLife
  • 319
  • 3
  • 13
  • 3
    This is a good question, and good answer. Why don't you add your update an answer so it's marked as answered? – Justin Fagnani Nov 09 '13 at 22:20
  • I got this from a [dart-sdk GitHub issue](https://github.com/dart-lang/sdk/issues/530#issuecomment-443135516): "For the record, since Dart 2.1, the bool class has had non-short-circuit operators &, | and ^." So nowadays it should work – Jefferson Quesado Nov 06 '19 at 18:09

2 Answers2

4

You can use the XOR operator on booleans since Dart version 2.1

[...] since Dart 2.1, the bool class has had non-short-circuit operators &, | and ^.

They can be used where you want both sides to be evaluated, and, especially for ^, they can be used in assignments: bool parity = false; while (something) parity ^= checkSomething();.

Taken from the corresponding Github issue.

See the dart documentation for XOR here.

josxha
  • 1,110
  • 8
  • 23
Guilherme Matuella
  • 2,193
  • 1
  • 17
  • 32
2

(Copied from the question, so that this appears as answered...)

Dart's spec only allows bitwise XOR on integers.

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275