【发布时间】:2010-08-26 05:53:23
【问题描述】:
我听说在 little endian 中,LSB 位于起始地址,而在 Big endian 中 MSB 位于起始地址。所以我这样写了我的代码。如果不是为什么?
void checkEndianess()
{
int i = 1;
char c = (char)i;
if(c)
cout<<"Little Endian"<<endl;
else
cout<<"Big Endian"<<endl;
}
【问题讨论】:
-
忽略所有这些丑陋的解决方案,变得性感:
int i = 1; if (reinterpret_cast<char&>(i)) { /* little */}。 -
@GMan:
if ( reinterpret_cast<char const&>( (int const&) 1) )在 GCC 上工作…… -
@Potato:哎呀,你说得对,我不性感。
if (reinterpret_cast<const char&>(static_cast<const int&>(1))) -
不要忘记
sizeof(int)>2有更多的方法来置换存储的字节......在你嘲笑这个想法之前,我曾经使用过一个存储所有整数类型小端的系统,但是floats 以混合顺序存储。
标签: c++ c endianness