大小端的定義無需贅言,常用的方法有使用聯合體和指針法,如:
int checkCPU() { union w { int a; char b; }c; c.a = 1; return (c.b == 1); // 小端返回TRUE,大端返回FALSE }
實際上Linux操作系統的源碼中,其判斷更為簡潔:
static union { char c[4]; unsigned long mylong; } endian_test = {{ 'l', '?', '?', 'b' } }; #define ENDIANNESS ((char)endian_test.mylong)