加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 有符号位字段表示

发布时间:2020-12-16 10:05:43 所属栏目:百科 来源:网络整理
导读:我使用字段大小为1的位创建了一个字段,并使用int而不是unsigned.后来,当我试图检查字段的值时,我发现值为-1. 我使用此代码检查二进制represantation和我的位字段的值: #include stdio.h#include stdlib.hunion { struct { int bit:1; } field; int rep;} n;
我使用字段大小为1的位创建了一个字段,并使用int而不是unsigned.后来,当我试图检查字段的值时,我发现值为-1.
我使用此代码检查二进制represantation和我的位字段的值:

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

union {
    struct {
        int bit:1;
    } field;
    int rep;
} n;

int main() {

int c,k;
n.field.bit=1;
 for (c = 31; c >= 0; c--)
  {
    k = n.rep >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("n %d n",n.field.bit);

return 0;
}

输出是:
00000000000000000000000000000001

-1

在那种情况下,为什么我的位字段的值是-1,当我使用signed int而不是unsigned时,它总是一个负数?

解决方法

你不应该使用plain int作为位域类型,如果你期望有关该值的东西,除了它可以容纳n位 – 根据C11标准,它实际上是实现定义的,是否在位字段中的int是有符号的未签名的 6.7.2p5:

5 Each of the comma-separated multisets designates the same type,except that for bit-fields,it is implementation-defined whether the specifier int designates the same type as signed int or the same type as unsigned int.

在您的情况下,int指定与signed int相同的类型;这是the default in GCC:

Whether a “plain” int bit-field is treated as a signed int bit-field or as an unsigned int bit-field (C90 6.5.2,C90 6.5.2.1,C99 and C11 6.7.2,C99 and C11 6.7.2.1).

By default it is treated as signed int but this may be changed by the -funsigned-bitfields option.

然后是实现定义有符号数是一个补码还是二进制补码 – 如果它们是一个补码,则可以存储在1位中的唯一值是符号位,因此为0;所以一位的有符号位字段在一个补码上没有意义.但是,您的系统使用2的补码 – 例如what GCC always uses:

Whether signed integer types are represented using sign and magnitude,two’s complement,or one’s complement,and whether the extraordinary value is a trap representation or an ordinary value (C99 and C11 6.2.6.2).

GCC supports only two’s complement integer types,and all bit patterns are ordinary values.

因此比特值1和0用带符号的二进制补码来解释:前者有符号位设置,所以它是负(-1)而后者没有符号位设置所以它是非负的(0).

因此,对于2比特的有符号比特字段,2的补码机器上的可能比特模式及其整数值是

> 00 – 具有int值0
> 01 – 具有int值1
> 10 – 具有int值-2
> 11 – 具有int值-1

在n比特字段中,最小有符号数是-2 ^(n-1),最大值是2 ^(n-1)-1.

现在,当对秩小于int的有符号整数操作数执行算术运算时,它首先被转换为int,因此值-1被符号扩展为全宽度int;默认参数促销也是如此;将值传递给printf时,该值将符号扩展为(全角)int.

因此,如果您希望从一位位域获得合理的值,请使用无符号位:1;或者,如果将其理解为布尔标志,则_Bool位:1;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读