Overview

Platforms with multi-byte objects must establish the object’s address and byte ordering. Objects are typically addressed by the smallest address of the bytes used. Bytes are ordered either in big-endian or little-endian. In big-endian, the most significant byte is listed first. In little-endian, the least significant byte is ordered first.

#include <stdint.h>
#include <stdio.h>
 
int main() {
  int32_t x = 0x01234567;
  for (int i = 0; i < 4; ++i) {
    printf("%.2x ", ((unsigned char *)(&x))[i]);
  }
}

The above snippet can be used to check endianness on the current machine. If big-endian, the output should be 01 23 45 67. If little-endian, 67 45 23 01.

Bibliography

  • Bryant, Randal E., and David O’Hallaron. Computer Systems: A Programmer’s Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.