Byte order
Byte order is the convention that assigns significance to the positions of bytes in a multibyte value.
Integers: Big-endian puts the most significant byte first; little-endian puts it last. For an unsigned 16-bit integer, hexadecimal bytes 01 02 mean 258 in big-endian and 513 in little-endian. Width, signedness and field layout must also be specified. [1]
Conversion: Reinterpreting the same bytes with a different byte-order descriptor differs from swapping the bytes. Starting from big-endian 01 02, changing both preserves 258 while changing either alone gives 513. NumPy exposes these as a dtype view and byteswap(), respectively. [2]
Arrays: Byte order within each element differs from C/Fortran element traversal. In NPY 1.0, the header-length field is little-endian independently of the payload dtype described by the header. [3]
Scalar extraction: For a plain unsigned 16-bit NumPy array, extracting an element returns a native-order scalar without changing the array. The scalar does not retain the source byte order. A simple dtype's byteorder may report = when an explicit order matches the host; inspect the array dtype and raw bytes together. [2] [5]
Relations
- related
- NumPy NPY format, Checksum, File storage
Citations
- Python struct: byte order, size and alignment
- NumPy: byte-swapping and dtype reinterpretation
- NumPy: NPY format and header fields
- NumPy: native and explicit dtype byte order
Further reading
Sources & caveats
- Source-reviewed terminology and exact unsigned 16-bit arithmetic; no device or corruption-rate measurement.
- Executable worked example: https://hesela.dev/research/byte-order.mjs; regression controls: https://hesela.dev/research/byte-order.test.mjs.
- Native NumPy scalar checks: https://hesela.dev/research/byte-order-native.py; the script reports its library version and host order and checks both explicit byte orders without hardware I/O.