有符号整数和无符号整数之间的区别是:

Unsigned值可以是一个较大的正数,而不能是负数。 Unsigned版本使用前导位作为值的一部分,而signed版本使用最左边的位来识别数字是正还是负。 有符号整数可以同时包含正数和负数。

还有其他区别吗?


当前回答

根据我们在课堂上学到的,有符号整数既可以表示正数也可以表示负数,而无符号整数只能表示非负数。

例如,看一个8位的数字:

无符号值为0到255

带符号的取值范围为-128 ~ 127

其他回答

Unsigned值可以是一个较大的正数,而不能是负数。

Yes.

Unsigned版本使用前导位作为值的一部分,而signed版本使用最左边的位来识别数字是正还是负。

有不同的表示有符号整数的方法。最容易想象的是使用最左边的位作为标志(符号和幅度),但更常见的是2的补位。这两种方法在大多数现代微处理器中都有使用——浮点数使用符号和幅度,而整数算术使用2的补数。

有符号整数可以同时包含正数和负数。

Yes.

在嵌入式系统上编程时,必须使用无符号整数。在循环中,当不需要有符号整数时,使用无符号整数将节省设计此类系统所需的安全。

除此之外,在C语言中,你不能溢出一个无符号整数;行为被定义为模算术。您可以溢出一个有符号整数,并且在理论上(尽管在当前主流系统上没有实践),溢出可能会触发一个错误(可能类似于除零错误)。

关于这个问题,我找到的最佳答案要感谢IBM引用的XDR标准:

Integer An XDR signed integer is a 32-bit piece of data that encodes an integer in the range [-2147483648,2147483647]. The integer is represented in two's complement notation. The most and least significant bytes are 0 and 3, respectively. The data description of integers is integer. Unsigned integer An XDR unsigned integer is a 32-bit piece of data that encodes a nonnegative integer in the range [0,4294967295]. It is represented by an unsigned binary number whose most and least significant bytes are 0 and 3, respectively. The data description of unsigned integers is unsigned.

参见维基百科上的XDR标准

是的,无符号整数可以存储大的值。 不,有不同的方式来表示正数和负数。 是的,有符号整数可以包含正数和负数。