深入理解计算机系统中第二章节有一习题:编写过程is_little_endian,当在小端机器上编译和运行时返回1,在大端法机器上编译运行时则返回0。这个程序应该可以运行在任何机器上,无论机器的字长是多少?
其参考答案如下:
There are many ways to solve this problem. The basic idea is to create some multibyte datum with different
values for the most and least-significant bytes. We then read byte 0 and determine which byte it is.
In the following solution is to create an int with value 1. We then access its first byte and convert it to an
int. This byte will equal 0 on a big-endian machine and 1 on a little-endian machine.