首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

考题解决思路

2012-03-03 
考题怎么确定CPU对操作数的存放方式,用的是Little-endian(从低字节到高字节)还是Big-endian?[解决办法]int

考题
怎么确定CPU对操作数的存放方式,用的是Little-endian(从低字节到高字节)还是Big-endian?

[解决办法]
int beorle()
{
 union u
 {
  WORD w;
  byte c;
 } t;
 t.w = 1;
 return t.c; //返回1为le 否则be
}
[解决办法]
#include <iostream>

using namespace std;

int main() {

union uT{
short a;
char b;
}u;

u.a = 0x1234;
if (0x12 == u.b) {
cout < < "stored in 0x12 little-endian 0x34 " < < endl;
} else if (0x34 == u.b) {
cout < < "stored in big-endian 0x34 0x12 " < < endl;
}
// 如果u.b == 0x12,则采用的是Little-endian,因为联合体占用同一个内存空间存放成员。
// 我的CPU用的是Big-endian,从高字节到低字节,因为u.b = 0x34,TGA的文件也是这样存的。
// 联合体的成员都是按地址顺序从低地址向高地址存放。
}
[解决办法]
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
union {
unsigned char a[4];
unsigned int b;
} c;

c.b = 0x000000ff;
if (c.a[0] == 0xff)
printf( "little endian\n ");
else
printf( "big endian\n ");

system( "pause ");
return 0;
}

热点排行