JavaScript Endian Encoding?

后端 未结 6 1283
清歌不尽
清歌不尽 2021-02-04 10:18

A response on SO got me thinking, does JavaScript guarantee a certain endian encoding across OSs and browsers?

Or put another way are bitwise shifts on integers \"safe\"

6条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 10:45

    are bitwise shifts on integers "safe" in JavaScript?

    Only for integers that fit within 32 bits (31+sign). Unlike, say, Python, you can't get 1<<40.

    This is how the bitwise operators are defined to work by ECMA-262, even though JavaScript Numbers are actually floats. (Technically, double-precision floats, giving you 52 bits of mantissa, easily enough to cover the range of a 32-bit int.)

    There is no issue of 'endianness' involved in bitwise arithmetic, and no byte-storage format where endianness could be involved is built into JavaScript.

提交回复
热议问题