字节截取

1 unsigned int a = 0xfffffff7;
2 unsigned char i = (unsigned char)a;
3 char *b = (char *)&a; 
4 printf("%x,%x",i,*b);

结果输出是000000f7和fffffff7;

unsigned int 四个字节,转换为unsigned char是1个字节,截取低位,则i为f7,由于是无符号所以x左边用0补齐32位

b是指针的转换,b指向的还是一个unsigned int的数,但是b是有符号的,则*b=f7,x左边用1补齐32位,则是fffffff7。

 1 #include
 2 #include
 3 using namespace std;
 4 int  main(){
 5     union w {
 6       int  a;
 7       char b;
 8     } c;
 9     c.a = 300;
10     bitset i(c.a);
11     bitset j(c.b);
12     cout << (int)c.b << endl;
13     cout << i << endl;
14     cout << j << endl;
15     return 0;
16 }

结果b的值是44.截取低8位

posted @ 2013-10-31 20:56  RunningPower  阅读(566)  评论(0编辑  收藏  举报