Endianness
Порядок байтов
Endianness determines which byte of a multi-byte number comes first: the most significant in big-endian, or the least significant in little-endian. It matters when reading raw memory, binary files, and network packets.
Also known as: big-endian, little-endian
What does byte order mean?
Suppose a file uses two bytes to store a count, and you read 01 02. Is the count 258 or 513? Both are possible. The answer depends on which byte carries the greater weight. In a two-byte unsigned integer, the high byte is multiplied by 256, while the low byte is added as-is. Endianness tells you which position holds which byte.
Big-endian vs. little-endian
Take the 32-bit integer 0x12345678. Each pair of hex digits represents one byte. 12 is the most significant byte and 78 is the least significant. Here is how the integer looks in a memory dump read from lowest to highest address:
| Byte order | Bytes in memory |
|---|---|
| Big-endian: most significant byte first | 12 34 56 78 |
| Little-endian: least significant byte first | 78 56 34 12 |
Neither representation changes the number itself. The CPU can perform the same arithmetic once the value is loaded correctly. Byte order does not reverse the bits inside a byte, and it makes no difference for a one-byte field.
Reading a binary field
Imagine a format that stores a payload length as a two-byte unsigned integer in big-endian order. In Python, you can make that choice explicit when reading and writing the field:
field = bytes.fromhex('01 02')
length = int.from_bytes(field, byteorder='big') # 258
encoded = length.to_bytes(2, byteorder='big') # b'\x01\x02'
Using 'little' in the first call returns 513, not an error. The bytes are valid either way; what makes the result correct is agreement with the format. The example assumes field contains exactly two bytes. A parser should check that it actually has the full field before decoding it.
When the choice matters
The CPU's native byte order and the data format's byte order are separate. An x86-64 machine normally stores integers in little-endian order, but it can still produce a TCP header, whose port fields use big-endian (also called network byte order). File formats make their own choices: for instance, a PNG chunk stores its length in big-endian, while a standard RIFF/WAVE chunk stores its size in little-endian. Use the format specification, not the host architecture, to decide how to encode a field.
Byte order also isn't the same as text encoding. In JSON, 258 is written as decimal digits, not as a fixed-width binary integer. A text encoding may have its own byte-order rules: UTF-16LE and UTF-16BE, for example, differ in how they store each 16-bit code unit.
What interviewers ask
A common exercise is to read the bytes 01 00 as an unsigned 16-bit integer in both orders: the answer is 256 in big-endian and 1 in little-endian. For a follow-up about converting a buffer, ask where the fields start and how wide they are. Swapping the bytes within each integer preserves field order; reversing the entire buffer does not. Test against an exact expected byte sequence with distinct bytes: a round-trip test alone can pass even when the writer and reader both use the same wrong order.