Skip to content
Search

Endianness

lastmod 2023-08-25created 2014-09-125 edits

Endianness refers to the way multiple consecutive items are arranged in a one-dimensional space such as computer memory; the way bytes are arranged in particular is called byte order.

Endianness is usually divided into big-endian, where the larger unit comes first, and little-endian, where the smaller unit comes first; arrangements that fall into neither case or that support both are sometimes called middle-endian.

Byte order

Byte order can be broadly divided into big-endian and little-endian. Big-endian puts the most significant byte first, the same way people write numbers, while little-endian, conversely, puts the least significant byte first. Some architectures such as the PDP-11 use different orders for 2-byte units and 1-byte units; these are called middle-endian. The following compares these methods.

Type

Representation of 0x1234 (decimal 4660)

Representation of 0x12345678 (decimal 305419896)

Big-endian

12 34

12 34 56 78

Little-endian

34 12

78 56 34 12

Middle-endian

-

34 12 78 56
or
56 78 12 34

Pros and cons

Big-endian tends to make debugging software easier. Because it matches the way people read and write numbers, memory values are easy to view during debugging; for example, 0x59654148 is represented in big-endian as 59 65 41 48.

Conversely, little-endian has the advantage that no extra computation is needed when using only the low-order bytes of a value stored in memory. For example, the 32-bit number 0x2A is represented in little-endian as 2A 00 00 00, and by taking just the first two bytes or one byte of this representation you can get the low-order 16 or 8 bits directly. In a 32-bit big-endian environment, on the other hand, you must add 2 or 3 bytes to the variable's address to get the low-order 16-bit or 8-bit value. Because the first byte of a variable is usually taken as its address, this property often makes programming easier, but it is also one reason programmers from little-endian environments often make mistakes in big-endian environments.

Also, since an adder performs addition starting from the LSB and computing carries, the adder design becomes a little simpler in little-endian, where the first byte is the LSB. In big-endian, the adder has to proceed backward from the last byte to the first byte when adding. However, today's processors read multiple bytes at once and perform the addition simultaneously, so there is practically no difference between the two endiannesses.

See also

Favorite site

author Zer0 sitemap