#include #include int main() { union { uint32_t u32; uint8_t u8[4]; } demo; demo.u32 = 0x12345678; /* * using the address, it depends on the byte ordering, what comes out. */ printf("lowest address: u8[0] = %hhx \n", demo.u8[0]); printf("highest address: u8[3] = %hhx \n", demo.u8[3]); /* * using math operations, the least significant byte can be masked or calculated * independently from the byte ordering. */ printf("least significant byte: %hhx \n", demo.u32 % 256); // modulo printf("least significant byte: %hhx \n", demo.u32 & 0xff); // bitwise AND printf("most significant byte: %hhx \n", demo.u32 / (256 * 256 * 256)); // division printf("most significant byte: %hhx \n", demo.u32 >> 24); // bit shift if (demo.u8[0] == demo.u32 % 256) { printf(" -> little endian\n"); } else { printf(" -> big endian\n"); } return 0; }