JavaScript Endian Encoding?

后端 未结 6 1279
清歌不尽
清歌不尽 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条回答
  • 2021-02-04 10:44

    ECMA Script does actually have a concept of an integer type but it is implicitly coerced to or from a double-precision floating-point value as necessary (if the number represented is too large or if it has a fractional component).

    Many mainstream Javascript interpreters (SpiderMonkey is an example) take a shortcut in implementation and interpret all numeric values as doubles to avoid checking the actual native type of the value for each instruction. As a result of the implementation hack, bit operations are implemented as a cast to an integral type followed by a cast back to a double representation. It is therefore not a good idea to use bit-level operations in Javascript and you won't get a performance boost anyway.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 11:00

    Yes, they are safe. Although you're not getting the speed benefits you might hope for since JS bit operations are "a hack".

    0 讨论(0)
  • 2021-02-04 11:02

    Some of these answers are dated, because endianness can be relevant when using typed arrays! Consider:

    var arr32 = new Uint32Array(1);
    var arr8 = new Uint8Array(arr32.buffer);
    arr32[0] = 255;
    console.log(arr8[0], arr8[1], arr8[2], arr8[3]);
    

    When I run this in Chrome's console, it yields 255 0 0 0, indicating that my machine is little-endian. However, typed arrays use the system endianness by default, so you might see 0 0 0 255 instead if your machine is big-endian.

    0 讨论(0)
  • 2021-02-04 11:08

    Shifting is safe, but your question is flawed because endianness doesn't affect bit-shift operations anyway. Shifting left is the same on big-endian and little-endian systems in all languages. (Shifting right can differ, but only due to interpretation of the sign bit, not the relative positions of any bits.)

    Endianness only comes into play when you have the option of interpreting some block of memory as bytes or as larger integer values. In general, Javascript doesn't give you that option since you don't get access to arbitrary blocks of memory, especially not the blocks of memory occupied by variables. Typed arrays offer views of data in an endian-sensitive way, but the ordering depends on the host system; it's not necessarily the same for all possible Javascript host environments.

    Endianness describes physical storage order, not logical storage order. Logically, the rightmost bit is always the least significant bit. Whether that bit's byte is the one that resides at the lowest memory address is a completely separate issue, and it only matters when your language exposes such a concept as "lowest memory address," which Javascript does not. Typed arrays do, but then only within the context of typed arrays; they still don't offer access to the storage of arbitrary data.

    0 讨论(0)
  • 2021-02-04 11:08

    JavaScript doesn't have an integer type, only a floating point type. You can never get close enough to the implementation details to worry about this.

    0 讨论(0)
提交回复
热议问题