CPU大小端的判定

大端:高字节存储在低地址,低字节存储在高地址

小端:低地址存储低字节,高地址存储高字节

#include 

bool IsLittle_Endian()
{
    union tmp
    {
        int a;
        char b;
    };
    tmp c;
    c.a = 1;
    return (c.b == 1);
}

bool IsBig_Endian()
{
    int a = 0x12345678;
    short p = (short)a;
    return (0x5678 == p);
}

int main(int argc, const char * argv[])
{
    if (IsLittle_Endian()) {
        std::cout << "小端" << std::endl;
    }else{
        std::cout << "大端" << std::endl;
    }
    
    if (IsBig_Endian())
    {
        std::cout << "小端" << std::endl;
    }else{
        std::cout << "大端" << std::endl;
    }
    return 0;
}


你可能感兴趣的:(每日一题)