Byte Ordering

Rukshani Athapathu
Coder's Corner
Published in
2 min readMay 15, 2018

In this brief post, we are going to talk about byte order.

Image Courtesy: Pixabay

A byte usually consists of 8 bits and and a collection of bytes form a word. Therefore, the bytes in a word can be numbered either from left to right or right to left. This convention of ordering bytes by a machine is called its byte order.

Think of a variable of an int type which has 32 bits(4 bytes) stored in a contiguous sequence of memory locations 0, 1, 2, 3. Let’s imagine the value of this variable to be 20555768 (in hexadecimal notation 0x0139A7F8) and the binary representation of that number is,

Little Endian

Some machines store bytes ordered from least significant byte to the most significant and that is called little endian. Here, the least significant byte has the hexadecimal value 0xF8 and the most significant byte 0x01.

Little Endian

Big Endian

In big endian format, bytes are ordered from most significant to the least significant.

Big Endian

As programmers, we rarely worry about byte ordering, but this becomes a problem when a little endian machine tries to send binary data to a big endian machine over the network and vice versa. As a solution , TCP/IP requires you to order the bytes in network byte order (which is big endian) before any binary integers in TCP/IP headers are sent across the network.

References

https://www.amazon.com/Structured-Computer-Organization-Andrew-Tanenbaum/dp/812034720X/ref=asap_bc?ie=UTF8

--

--