【问题标题】:what does this mean in c int a:16;? [duplicate]这在 c int a:16; 中是什么意思? [复制]
【发布时间】:2011-01-16 16:34:01
【问题描述】:

可能重复:
What does 'unsigned temp:3' mean?

请问这个符号是什么意思

int a:16;

我发现它是这样的代码,它确实可以编译。

结构名称{ 诠释:16; }

【问题讨论】:

    标签: c++ c programming-languages annotations


    【解决方案1】:

    这是bitfield

    这个特定的位域没有多大意义,因为您只能使用 16 位类型,并且您正在浪费一些空间,因为位域被填充到 int 的大小。

    通常,您将它用于包含位大小元素的结构:

    struct {
        unsigned nibble1 : 4;
        unsigned nibble2 : 4;
    }
    

    【讨论】:

      【解决方案2】:
      struct name { int a:16; }
      

      这意味着a被定义为16位内存空间。 int 的剩余位(16 位)可用于定义另一个变量,例如 b,如下所示:

      struct name { int a:16;  int b:16; }
      

      所以如果int是32位(4字节),那么一个int的内存就分为ab两个变量。

      PS:我假设sizeof(int) = 4 字节,1 字节 = 8 位

      【讨论】:

        【解决方案3】:
         struct s
            {
             int a:1;
             int b:2;
             int c:7;
            };/*size of structure s is 4 bytes and not 4*3=12 bytes since all share the same space provided by int declaration for the first variable.*/
         struct s1
            {
             char a:1;
             };/*size of struct s1 is 1byte had it been having any more char _var:_val it would have    been the same.*/
        

        【讨论】:

          【解决方案4】:

          这是一个位域。

          我从未见过 16 位位域;通常很短。

          http://www.cs.cf.ac.uk/Dave/C/node13.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-16
            • 2011-04-16
            • 2014-12-21
            • 1970-01-01
            • 2021-03-31
            • 2021-09-18
            • 1970-01-01
            相关资源
            最近更新 更多