判断机器大小端的两种实现方式

方式一

  使用两个静态字节,代码较长

方式二

  使用三个静态字节,代码简短

 1 bool is_big_endian1(){
 2     static union tmp_u{
 3         tmp_u():s(0x0100){}
 4         short s;
 5         char b;
 6     } tmp;
 7 
 8     return tmp.b;
 9 }
10 
11 bool is_big_endian2(){
12     static short s = 0x0100;
13     static char b = (*(char*)&s);
14 
15     return b;
16 }

 

posted @ 2021-11-28 08:58  蜗牛牛  阅读(38)  评论(0编辑  收藏  举报