简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM