Sub topic 8
Byte Order: Little-Endian and Big-Endian
This chapter assumes you know how to create an ArrayBuffer, how to put several typed array views over it, and how to read numbers in hexadecimal. In the previous chapter you saw two views share one buffer. Here you will see why the bytes those views expose can appear in an order you did not expect.
The problem: one number, several bytes
A Uint32Array element takes 4 bytes. When you store 0x01020304, the engine has to choose which of the four memory addresses gets each of the bytes 01, 02, 03, and 04. That choice is called byte order, or endianness.
- Big-endian puts the most significant byte first, at the lowest address. The bytes read
01 02 03 04, the same way you write the number. - Little-endian puts the least significant byte first. The bytes read
04 03 02 01.
Typed arrays use the platform's order
Typed arrays read and write multi-byte elements in the native byte order of the machine running the code. Almost all current hardware (x86-64 and ARM in its usual configuration) is little-endian, so in practice you will nearly always see little-endian results. The specification does not promise this, though.
const buf = new ArrayBuffer(4);
const words = new Uint32Array(buf);
const bytes = new Uint8Array(buf);
words[0] = 0x01020304;
console.log(Array.from(bytes, b => b.toString(16))); // [ '4', '3', '2', '1' ] on little-endianYou can detect the platform order at runtime:
const isLittleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1;
console.log(isLittleEndian); // true on most hardwareWhere byte order matters
Byte order only matters when bytes cross a boundary between your code and something else.
| Situation | Does order matter? |
|---|---|
| Writing and reading through the same typed array type | No |
| Viewing the same buffer through different element sizes | Yes |
| Reading a file format or network protocol | Yes, the format defines it |
| Passing a buffer to WebGL or WebAssembly | Usually little-endian is expected |
Network protocols traditionally use big-endian, which is often called "network byte order". Many file formats, such as BMP and WAV, use little-endian. PNG uses big-endian.
Assembling bytes by hand
If you only have a Uint8Array, you can build a multi-byte value in a known order with bit operations:
const data = new Uint8Array([0x12, 0x34]);
const bigEndian = (data[0] << 8) | data[1];
const littleEndian = data[0] | (data[1] << 8);
console.log(bigEndian.toString(16)); // "1234"
console.log(littleEndian.toString(16)); // "3412"This approach works, but it is tedious for 32-bit and floating-point values. A common mistake is that << produces a signed 32-bit result, so (0xff << 24) is negative. You must add >>> 0 to get an unsigned value. The next chapter introduces DataView, which lets you name the byte order on every read and write, so you do not have to do this by hand.
Exercise
On a little-endian machine, you run const u16 = new Uint16Array([0x0A0B]); const b = new Uint8Array(u16.buffer);. What are b[0] and b[1] in decimal?
Show answer
b[0] is 11 (0x0B) and b[1] is 10 (0x0A), because little-endian stores the least significant byte at the lowest offset.
Exercise
A protocol sends a 16-bit big-endian length as the bytes [0x01, 0x00] in a Uint8Array called p. Write an expression that produces the length, and give its value.
Show answer
(p[0] << 8) | p[1], which evaluates to 256.