Endianness is the order in which a machine or format stores the bytes of a multi-byte value. It does not reverse the bits inside each byte. For the 32-bit value 0x12345678, the bytes are 12, 34, 56, and 78; only their address order changes.

AddressBig-endian byteLittle-endian byte
10000x120x78
10010x340x56
10020x560x34
10030x780x12

Big-endian puts the most significant byte at the lowest address. Little-endian puts the least significant byte there. The distinction matters when bytes cross a boundary: a network protocol, binary file, device register, or foreign-function interface. Within a process, ordinary integer operations hide the storage order.

Make the boundary explicit

Never serialize by copying the in-memory representation of an integer and assuming the receiver uses the same order. Define the order in the format and use an API that names it:

using System.Buffers.Binary;
 
Span<byte> frame = stackalloc byte[4];
BinaryPrimitives.WriteUInt32BigEndian(frame, 0x12345678);
 
uint correct = BinaryPrimitives.ReadUInt32BigEndian(frame);    // 0x12345678
uint swapped = BinaryPrimitives.ReadUInt32LittleEndian(frame); // 0x78563412

Many Internet protocols use network byte order, which is big-endian. File formats are free to choose either order, and some carry a byte-order marker. CPU families are not a reliable protocol contract: an architecture can support more than one mode, while an operating-system ABI normally chooses one. BitConverter.IsLittleEndian reports the current runtime’s convention when native layout genuinely matters; it is not a substitute for an explicit wire format.

Pitfalls

  • Text has character encoding, not integer endianness, until a text encoding defines multi-byte code units or a byte-order marker.
  • Hex dumps show bytes in increasing address order. A little-endian integer can therefore look “reversed” even though the dump is correct.
  • A struct’s byte layout also includes alignment and padding. Matching byte order alone does not make raw struct serialization portable.

References