您的位置:首页 > 其它

如何判断大端小端?

2014-08-20 20:28 232 查看
一、最简单的做法:

参考(深入理解计算机系统中文版第二版,P28,show_bytes)

转化成usigned char*的byte_pointer;

然后遍历输出每个字节的值,即可判断。

输入可以是任意一个数。

类似于:http://blog.csdn.net/yuucyf/article/details/7193148

二、利用联合

由于联合是共享地址的,所以可以采用如下方式:

#include <iostream>
using namespace std;

union
{
int number;
char s;
}test;

bool testBigEndin()
{
test.number=0x01000002;
return (test.s==0x01);
}

void main()
{
if (testBigEndin())
cout<<"big"<<endl;
else
cout<<"small"<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: