最近遇上好幾次關於大端序、小端序的問題,記混了,想了好半天,得虧寫對了 。今天總結一下,希望記住了。
大端序:數據的高位保存在內存的低地址中,而數據的低位,保存在內存的高地址中
小端序:數據的高位保存在內存的高地址中,而數據的低位,保存在內存的低地址中
測試編譯器是大端序還是小端序的代碼:
#include <iostream>
using namespace std;
int main(void)
{
int a = 0x12345678;
char *p=(char *)&a;//指針是指向低地址的
if(0x78==*p)
{
cout<<"little endian"<<endl;
}
else
{
cout<<"big endian"<<endl;
}
return 0;
}