关于大小端的题目

有个问题是关于位字段 和 大小端的问题,是一个笔试题。
C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
struct  stucA{
     unsigned  int  a:1;
     unsigned  int  b:2;
     unsigned  int  c:3;
     unsigned  int  d:4;
     unsigned  int  e:5;
     unsigned  int  f:6;
     unsigned  int  g:11;
}A;
memset (&A,0, sizeof (A));
A.d=11;


求变量A在内存中的表示,按照大端、小端方式分别写出结果。
    | BYTE0   BYTE1   BYTE2   BYTE3
----+-------------------------------
大端|
----+-------------------------------
小端|

------------------------------------




BIG INDIAN
unsigned int 表示(位数从高位算起)
00000011 01000000 00000000 00000000
字节表示
00 00 02 C0

LITTLE INDIAN
unsigned int 表示(位数从低位算起)
00000000 00000000 00000010 11000000
字节表示
C0 02 00 00

C/C++ code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <string.h>
 
struct  bits {
     unsigned  int  a:1;
     unsigned  int  b:2;
     unsigned  int  c:3;
     unsigned  int  d:4;
     unsigned  int  e:5;
     unsigned  int  f:6;
     unsigned  int  g:11;
} bits;
 
struct  chars {
         unsigned  char  c[4];
};
 
union  ubc {
         struct  bits   b;
         struct  chars  c;
         unsigned      u;
} ubc;
 
int  main( int  argc,  char  *argv[])
{
         int  i;
 
         memset (&ubc,0, sizeof (ubc));
         ubc.b.d = 11;
 
         for  (i = 0; i < 4; i++)
                 printf ( "%02x" , (unsigned)(unsigned  char )ubc.c.c[i]);
         printf ( "\n" );
         printf ( "%08x\n" , ubc.u);
 
         return  0;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值