#include <stdio.h>
//A byte pointer refers to a sequence of bytes
typedef unsigned char * byte_pointer;
//The input is a byte pointer and a byte number
bool is_little_endian(){
int depozit;
short x = 29; //29 = 0x00 1d
byte_pointer start = (byte_pointer)&x;
depozit = start[0];
return depozit==0x1d;
}
int main() {
printf("%d\n",is_little_endian());
return 0;
}
intel x86-64 小端机器上
win64
linux64
先在linux中第二行加上红色框框代码,linux中出现bool类型需要添加的头文件。
运行结果如下:
官方答案:
int is_little_endian(){
int test_num = 0xff; //0x00 00 00 ff
byte_pointer byte_start = (byte_pointer) &test_num;
if(byte_start[0] == 0xff){
return 1;
}
return 0;
}