在C(什么是输出)无符号和符号值 [英] Unsigned and signed values in C (what is the output)
问题描述
signed int x= -5;
unsigned int y=x;
是什么y的值?怎么样?
what is the value of y? and how?
推荐答案
这取决于的最大值unsigned int类型
。通常情况下, unsigned int类型
是32位长,所以 UINT_MAX
2 32 &减去; 1. C标准(宗; 6.3.1.3/2)需要一个有符号和RARR;无符号的转换作为
It depends on the maximum value of the unsigned int
. Typically, a unsigned int
is 32-bit long, so the UINT_MAX
is 232 − 1. The C standard (§6.3.1.3/2) requires a signed → unsigned conversion be performed as
否则,如果新类型是无符号的值被重复地增加或减1比可以重新$ P $在新型psented直到该值是在新的类型的范围的最大值更转换
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.
因此Y = X +((2 32 &减; 1)+ 1)= 2 32 &减去; 5 = 4294967291。
Thus y = x + ((232 − 1) + 1) = 232 − 5 = 4294967291.
在一个 2的补平台,其中大部分实施的是的今天, 是
也是一样的2的补再$ p $ X
。
In a 2's complement platform, which most implementations are nowadays, y
is also the same as 2's complement representation of x
.
= -5〜5 + 1 = 0xFFFFFFFA + 1 = 0xFFFFFFFB = 4294967291。
-5 = ~5 + 1 = 0xFFFFFFFA + 1 = 0xFFFFFFFB = 4294967291.
这篇关于在C(什么是输出)无符号和符号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!