免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 姚世友
打印 上一主题 下一主题

[C] 考考大家的C语言基础  关闭 [复制链接]

论坛徽章:
0
81 [报告]
发表于 2008-12-22 22:13 |只看该作者

回复 #62 Aquester 的帖子

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv)
{
    union
    {
          struct  
          {
              char a:1;         
              char b:2;         
              char c:3;        
          }d;
        char e;
    } f;
   
    f.e = 1;
    printf("%d\n",f.d.a);
        printf("%c\n",f.d.a);
       
        printf("%d\n",f.d.b);
        printf("%c\n",f.d.b);
       
        printf("%d\n",f.d.c);
        printf("%c\n",f.d.c);
          
    getchar();
    return 0;
}

输出结果为:
-1

0

0

怎么解释啊,
谢谢

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
82 [报告]
发表于 2008-12-22 22:56 |只看该作者
这段英语说的很清楚!!!!


6.4. Bitfields
While we're on the subject of structures, we might as well look at bitfields. They can only be declared inside a structure or a union, and allow you to specify some very small objects of a given number of bits in length. Their usefulness is limited and they aren't seen in many programs, but we'll deal with them anyway. This example should help to make things clear:

struct {
      /* field 4 bits wide */
      unsigned field1 :4;
      /*
       * unnamed 3 bit field
       * unnamed fields allow for padding
       */
      unsigned        :3;
      /*
       * one-bit field
       * can only be 0 or -1 in two's complement!
       */
      signed field2   :1;
      /* align next field on a storage unit */
      unsigned        :0;
      unsigned field3 :6;
}full_of_fields;Example 6.13
Each field is accessed and manipulated as if it were an ordinary member of a structure. The keywords signed and unsigned mean what you would expect, except that it is interesting to note that a 1-bit signed field on a two's complement machine can only take the values 0 or -1. The declarations are permitted to include the const and volatile qualifiers.

The main use of bitfields is either to allow tight packing of data or to be able to specify the fields within some externally produced data files. C gives no guarantee of the ordering of fields within machine words, so if you do use them for the latter reason, you program will not only be non-portable, it will be compiler-dependent too. The Standard says that fields are packed into ‘storage units’, which are typically machine words. The packing order, and whether or not a bitfield may cross a storage unit boundary, are implementation defined. To force alignment to a storage unit boundary, a zero width field is used before the one that you want to have aligned.

Be careful using them. It can require a surprising amount of run-time code to manipulate these things and you can end up using more space than they save.

Bit fields do not have addresses—you can't have pointers to them or arrays of them.

论坛徽章:
0
83 [报告]
发表于 2008-12-23 09:06 |只看该作者
这个程序不会显示任何东西!!!1

论坛徽章:
0
84 [报告]
发表于 2008-12-23 11:43 |只看该作者
f.e = 1, 由于union结构其在内存中共享内存空间,分配了1个字节存放e,也就是0001共4位,调用f.d.a时,a的空间占一位也就是1,0就变成其符号位,所以答案是-1.如果f.e = 2时,0010, 所以a就为-0,也就是0;

如果char a 声明为unsinged char a 的话,f.d.a = 1.

不知道这么理解对吗?

论坛徽章:
0
85 [报告]
发表于 2008-12-23 13:19 |只看该作者
原帖由 火部 于 2008-12-23 11:43 发表
f.e = 1, 由于union结构其在内存中共享内存空间,分配了1个字节存放e,也就是0001共4位,调用f.d.a时,a的空间占一位也就是1,0就变成其符号位,所以答案是-1.如果f.e = 2时,0010, 所以a就为-0,也就是0;

...


char -128~127
unsigned char 0~255

只是一个符号位,应该不会受影响.

论坛徽章:
0
86 [报告]
发表于 2008-12-23 13:20 |只看该作者

回复 #81 dragonfly0427 的帖子

论坛徽章:
0
87 [报告]
发表于 2008-12-23 13:21 |只看该作者

回复 #82 goldenfort 的帖子

这段很重要.收藏了.Thanks.


结贴走人.

论坛徽章:
0
88 [报告]
发表于 2008-12-23 14:09 |只看该作者
收藏此帖~~

论坛徽章:
0
89 [报告]
发表于 2008-12-23 17:45 |只看该作者
原帖由 o_distance 于 2008-12-20 00:29 发表
答案前面的兄弟已经给了很好的解答。

这种涉及位域的题目,一直是很多c程序员面试的必考题,属于基础。这道题是典型。我们先不说不同系统关于字节序的争议。很多人知道位域,但他不一定能了解想到类型转换, ...

登录上来顶这句话

论坛徽章:
0
90 [报告]
发表于 2008-12-23 17:57 |只看该作者
大端小端会有影响的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP