Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Endian Encoding?

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" in JavaScript?

like image 394
Allain Lalonde Avatar asked Feb 02 '09 17:02

Allain Lalonde


People also ask

Is Javascript big-endian or little-endian?

As almost all desktop computers are x86, this means little-endian.

What is an endian format?

Big-endian is an order in which the "big end" (most significant value in the sequence) is stored first, at the lowest storage address. Little-endian is an order in which the "little end" (least significant value in the sequence) is stored first.

Is big-endian read left to right?

A big-endian representation has a multibyte integer written with its most significant byte on the left; a number represented thus is easily read by English-speaking humans. A little-endian representation, on the other hand, places the most significant byte on the right.

What is big-endian encoding?

Big Endian byte ordering is defined as follows: In a binary number consisting of multiple bytes, the most significant byte shall be encoded first; with the remaining bytes encoded in decreasing order of significance.


5 Answers

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.

like image 108
Rob Kennedy Avatar answered Sep 30 '22 03:09

Rob Kennedy


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.

like image 31
Mike Keesey Avatar answered Sep 30 '22 03:09

Mike Keesey


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

like image 43
Michael Haren Avatar answered Sep 30 '22 03:09

Michael Haren


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.

like image 35
Michael Roberts Avatar answered Sep 30 '22 03:09

Michael Roberts


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.

like image 37
bobince Avatar answered Sep 30 '22 02:09

bobince