Kawaljeet Singh
Kawaljeet Singh

Reputation: 35

Why left-shift in JS and Dart are different?

In Javascript: 255 << 24 = -16777216

In dart: 255 << 24 = 4278190080

Is there any way by which I get the same answer in Dart similar to JS ?

Upvotes: 0

Views: 139

Answers (1)

lrn
lrn

Reputation: 71763

To get precisely the same result in Dart as in JavaScript (whether on the web or not), do:

  var jsValue = (255 << 24).toSigned(32);

JavaScript converts all bitwise operations to 32-bit integers, and to signed integers for all operators except >>>. So, do .toSigned(32) on the result to do precisely what JavaScript does.

Upvotes: 2

Related Questions