一般计算机中内存的基本分配单位是字节(BYTE),1字节一般8bit,内存地址编号以字节大小递增。
所谓大端模式,表示的是数据类型大于1字节(int, long等)的数据在内存中低地址存放高位字节的数据。
例如,int iB = 0x12345678; 内存组织为:(低地址)0x12,0x34,0x56,0x78;
所谓小端模式,表示的是数据类型大于1字节(int, long等)的数据在内存中低地址存放低位字节的数据。
例如,int iL = 0x12345678; 内存组织为:(低地址)0x78,0x56,0x34,0x12;
short a[] = {0x1234, 0x5678, 0x3456, 0x6543};
char *ca = (char *)a;
int *ia = (int *)a;
cout << hex << (int)ca[0] << endl; //0x34
cout << hex << (int)ca[1] << endl; //0x12
cout << hex << (int)ca[2] << endl; //0x78
cout << hex << (int)ca[3] << endl; //0x56
cout << hex << (int)ca[4] << endl; //0x56
cout << hex << (int)ca[5] << endl; //0x34
cout << hex << (int)ca[6] << endl; //0x43
cout << hex << (int)ca[7] << endl; //0x65
cout << hex << (int)ia[0] << endl; //0x56781234
cout << hex << (int)ia[1] << endl; //0x65433456
网友评论