整数提升和无符号解释

integer promotion and unsigned interpretation

int_8 int8     = ~0;
uint_16 uInt16 = (uint_16) int8;

关于上面的类型转换;在 C 标准中,我可以在哪里找到对以下行为的指示的参考? - 在无符号解释 (uInt16=0xFFFF) 之前对较大类型进行符号扩展,而不是在对较大类型 (uInt16=0xFF) 进行 0 扩展后进行无符号解释。

来自 C99 6.3.1.8

否则,如果无符号整数类型的操作数的秩大于或等于另一个操作数类型的秩,则将有符号整数类型的操作数转换为无符号整数类型的操作数。

上面的陈述很清楚需要转换哪个变量,但是对于实际应该如何执行对话并不是很清楚,因此我的问题要求从标准中获得参考。 谢谢

根据 standard:

6.3.1.3 Signed and unsigned integers

......
2. Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

以及为避免在解释上述内容时造成混淆的脚注:

The rules describe arithmetic on the mathematical value, not the value of a given type of expression.

即如果您的 int8 的值为 -1(假设负数表示是 2 的补码,在您的示例中就是这样),当转换为 uint16_t 时,值 (0xFFFF + 1) 将是添加到它(比 uint16_t 可以表示的最大值多一个),产生 0xFFFF + 1 - 1 = 0xFFFF.

的结果

我认为答案实际上也是 6.3.1.8 的一部分:

否则,对两个操作数执行整数提升。 然后以下规则应用于提升的操作数: .... 否则,如果具有无符号整数类型的操作数的等级大于或等于另一个操作数类型的等级.....

意味着在使用规则 6.3.1.3 转换为无符号数之前首先执行整数提升。