对无符号和符号整数算术运算 [英] Arithmetic operations on unsigned and signed integers

查看:336
本文介绍了对无符号和符号整数算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅此code段

int main()
{ 
 unsigned int a = 1000;
 int b = -1;
 if (a>b) printf("A is BIG! %d\n", a-b);
 else printf("a is SMALL! %d\n", a-b); 
 return 0;
}   

这使输出:一个小:1001

This gives the output: a is SMALL: 1001

我不明白这里发生了什么。如何在>运营商在这里工作?为什么是一个比B小吗?如果它确实是小,为什么我得到一个正数(1001)的区别?

I don't understand what's happening here. How does the > operator work here? Why is "a" smaller than "b"? If it is indeed smaller, why do i get a positive number (1001) as the difference?

推荐答案

不同的整数类型之间的二进制操作是通过这样定义的普通类型,称为内进行的通常的算术转换的(参见语言规范,6.3.1.8)。在你的情况下,普通的类型是 unsigned int类型。这意味着 INT 操作数(你的 B )将被转换为 unsigned int类型比较之前,以及用于进行减法的目的

Binary operations between different integral types are performed within a "common" type defined by so called usual arithmetic conversions (see the language specification, 6.3.1.8). In your case the "common" type is unsigned int. This means that int operand (your b) will get converted to unsigned int before the comparison, as well as for the purpose of performing subtraction.

1 转换为 unsigned int类型的结果是最大可能符号INT 值(与 UINT_MAX )。不用说,这将是比你的无符号的 1000 值时,这意味着 A> b 的确是假的, A 确实的的相比(无符号)b 。在如果在code应解析为其他分支,它是你在实验中观察到的东西。

When -1 is converted to unsigned int the result is the maximal possible unsigned int value (same as UINT_MAX). Needless to say, it is going to be greater than your unsigned 1000 value, meaning that a > b is indeed false and a is indeed small compared to (unsigned) b. The if in your code should resolve to else branch, which is what you observed in your experiment.

相同的转换规则适用于减法。你的 AB 是PTED为真间$ P $ A - (无符号)b 和结果类型 unsigned int类型。这样的价值不能用%d个格式说明,打印,因为%d个只适用于的签署值。您试图用%d个结果不确定的行为,这样你就能看到印(即使它在实践中的逻辑确定性的解释)的数值打印是从完全没有意义观点C语言。

The same conversion rules apply to subtraction. Your a-b is really interpreted as a - (unsigned) b and the result has type unsigned int. Such value cannot be printed with %d format specifier, since %d only works with signed values. Your attempt to print it with %d results in undefined behavior, so the value that you see printed (even though it has a logical deterministic explanation in practice) is completely meaningless from the point of view of C language.

编辑:其实,我可能是错关于未定义行为的一部分。根据C语言的规范,相应的符号和无符号整型的范围内的公共部分应具有相同的重presentation(这意味着,根据脚注31,互换性作为参数传递给函数)。所以,结果 A - B 前pression是无符号的 1001 如上所述,除非我中号失去了一些东西,它是合法的打印与这个特定的符号值%d个说明,因为它属于 INT 。印刷(无符号)INT_MAX + 1 %d个将是不确定的,但 1001u 的罚款。

Actually, I could be wrong about the undefined behavior part. According to C language specification, the common part of the range of the corresponding signed and unsigned integer type shall have identical representation (implying, according to the footnote 31, "interchangeability as arguments to functions"). So, the result of a - b expression is unsigned 1001 as described above, and unless I'm missing something, it is legal to print this specific unsigned value with %d specifier, since it falls within the positive range of int. Printing (unsigned) INT_MAX + 1 with %d would be undefined, but 1001u is fine.

这篇关于对无符号和符号整数算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆