本文介绍了无符号位字段类型:int或unsigned int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99标准的第6.3.1.1节包括:

Section 6.3.1.1 of the C99 standard contains:

以下可在使用
  前pression只要一个 INT
   unsigned int类型可用于:

[...]类型的位字段 _Bool
   INT 符号int 符号
  INT

[...] A bit-field of type _Bool, int, signed int, or unsigned int.

如果一个 INT 可以重新present所有值
  原始类型的,这个值是
  转换到 INT ;否则,它
  被转换为 unsigned int类型

If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int.

在我看来,这意味着 unsigned int类型位字段都提升到 INT ,除了当无符号位字段的宽度等于 INT 的宽度,在这种情况下,最后一句适用。

It seems to me that this implies that unsigned int bit-fields are promoted to int, except when the width of the unsigned bit-field is equal to the width of int, in which case the last phrase applies.

我有以下程序:

struct S { unsigned f:32; } x = { 28349};

unsigned short us = 0xDC23L;

main(){
  int r = (x.f ^ ((short)-87)) >= us;
  printf("%d\n", r);
  return r;
}

和两个系统执行该程序( INT 是这两个系统上的32位)。一个系统说,这种程序打印1,另说,它打印0.我的问题是,针对的两个系统,我应该提交错误报告? (我倾向于提出反对,打印0系统的报告,因为摘录以上)

And two systems to execute this program (int is 32-bit on both systems). One system says this program prints 1, and the other says that it prints 0. My question is, against which of the two systems should I file a bug report? (I am leaning towards filing the report against the system that prints 0, because of the excerpt above)

推荐答案

看来,这种不确定性已经由标准委员会,因为目前的草案明确了那句话检测:

It seems that this ambiguity has already been detected by the standards committee since the current draft clarifies that sentence:

如果int可以重新present的所有值
  原始类型(如通过限制
  宽度,对于一个位域),值
  被转换为int;

这篇关于无符号位字段类型:int或unsigned int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:30