有符号变量和无符号变量有什么区别?
我已经在 C 和 C++ 的上下文中看到过这些内容,但是有符号变量和无符号变量有什么区别?
I have seen these mentioned in the context of C and C++, but what is the difference between signed and unsigned variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有符号变量,例如有符号整数,允许您表示正负范围内的数字。
无符号变量,例如无符号整数,仅允许您表示正数和零。
相同类型的无符号和有符号变量(例如
int
和byte
)都具有相同的范围(范围分别为65,536和256个数字),但是无符号可以表示比相应的有符号变量更大的数值。例如,
无符号字节
可以表示从0
到255
的值,而有符号字节
可以表示-128 至
127
。维基百科页面上的有符号数字表示解释了位级别表示的差异,并且整数(计算机科学) 页面提供了每个有符号/无符号整数类型的范围表。
Signed variables, such as signed integers will allow you to represent numbers both in the positive and negative ranges.
Unsigned variables, such as unsigned integers, will only allow you to represent numbers in the positive and zero.
Unsigned and signed variables of the same type (such as
int
andbyte
) both have the same range (range of 65,536 and 256 numbers, respectively), but unsigned can represent a larger magnitude number than the corresponding signed variable.For example, an
unsigned byte
can represent values from0
to255
, whilesigned byte
can represent-128
to127
.Wikipedia page on Signed number representations explains the difference in the representation at the bit level, and the Integer (computer science) page provides a table of ranges for each signed/unsigned integer type.
虽然通常称为“符号位”,但我们通常使用的二进制值没有真正的符号位。
大多数计算机使用补码算术。 负数是通过取反码(翻转所有位)并加一来创建的:
5(十进制)->5 00000101(二进制)
1 的补码:11111010
添加 1: 11111011,即十六进制的“FB”
这就是为什么有符号字节保存从 -128 到 +127 而不是 -127 到 +127 的值:
1 0 0 0 0 0 0 0 = -128
1 0 0 0 0 0 0 1 = -127
- - -
1 1 1 1 1 1 1 0 = -2
1 1 1 1 1 1 1 1 = -1
0 0 0 0 0 0 0 0 = 0
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 1 0 = 2
- - -
0 1 1 1 1 1 1 0 = 126
0 1 1 1 1 1 1 1 = 127
(将 1 加到 127 得到:)
1 0 0 0 0 0 0 0
我们在此图表顶部看到的是 -128。如果我们有一个正确的符号位,则值范围将是相同的(例如,-127 到 +127),因为为符号保留了一位。 如果最高有效位是符号位,则我们有:
5(十进制)-> 00000101(二进制)
<代码>-5(十进制)-> 10000101(二进制)
在这种情况下有趣的是我们同时有一个零和一个负零:
0(十进制)-> 00000000(二进制)
<代码>-0(十进制)-> 10000000(二进制)
我们没有带有补码的 -0; -0 是 -128(或者更一般地说,比最大正值多 1)。 不过,我们用补语来做; 所有 1 位都是负 0。
从数学上讲,-0 等于 0。我依稀记得有一台计算机,其中 -0 < 0,但我现在找不到任何参考。
While commonly referred to as a 'sign bit', the binary values we usually use do not have a true sign bit.
Most computers use two's-complement arithmetic. Negative numbers are created by taking the one's-complement (flip all the bits) and adding one:
5 (decimal) -> 00000101 (binary)
1's complement: 11111010
add 1: 11111011 which is 'FB' in hex
This is why a signed byte holds values from -128 to +127 instead of -127 to +127:
1 0 0 0 0 0 0 0 = -128
1 0 0 0 0 0 0 1 = -127
- - -
1 1 1 1 1 1 1 0 = -2
1 1 1 1 1 1 1 1 = -1
0 0 0 0 0 0 0 0 = 0
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 1 0 = 2
- - -
0 1 1 1 1 1 1 0 = 126
0 1 1 1 1 1 1 1 = 127
(add 1 to 127 gives:)
1 0 0 0 0 0 0 0
which we see at the top of this chart is -128.If we had a proper sign bit, the value range would be the same (e.g., -127 to +127) because one bit is reserved for the sign. If the most-significant-bit is the sign bit, we'd have:
5 (decimal) -> 00000101 (binary)
-5 (decimal) -> 10000101 (binary)
The interesting thing in this case is we have both a zero and a negative zero:
0 (decimal) -> 00000000 (binary)
-0 (decimal) -> 10000000 (binary)
We don't have -0 with two's-complement; what would be -0 is -128 (or to be more general, one more than the largest positive value). We do with one's complement though; all 1 bits is negative 0.
Mathematically, -0 equals 0. I vaguely remember a computer where -0 < 0, but I can't find any reference to it now.
有符号变量使用一位来标记它们是正数还是负数。 无符号变量没有这个位,因此它们可以在同一空间中存储更大的数字,但只能存储非负数,例如0和更大的数字。
有关更多信息:无符号和有符号整数
Signed variables use one bit to flag whether they are positive or negative. Unsigned variables don't have this bit, so they can store larger numbers in the same space, but only nonnegative numbers, e.g. 0 and higher.
For more: Unsigned and Signed Integers
无符号变量只能是正数,因为它们缺乏指示它们是负数的能力。
这种能力称为“符号”或“签名位”。
副作用是,如果没有签名位,它们就会多一个可用于表示数字的位,使其可表示的最大数字加倍。
Unsigned variables can only be positive numbers, because they lack the ability to indicate that they are negative.
This ability is called the 'sign' or 'signing bit'.
A side effect is that without a signing bit, they have one more bit that can be used to represent the number, doubling the maximum number it can represent.
有符号变量可以是 0、正数或负数。
无符号变量可以是 0 或正数。
有时使用无符号变量,因为可以使用更多位来表示实际值。 给你更大的范围。 例如,您还可以确保负值不会传递给您的函数。
Signed variables can be 0, positive or negative.
Unsigned variables can be 0 or positive.
Unsigned variables are used sometimes because more bits can be used to represent the actual value. Giving you a larger range. Also you can ensure that a negative value won't be passed to your function for example.
当 ur 值必须为正数时使用 unsigned,此处不能为负值,
如果有符号 int 范围为 -32768 到 +32767
如果 int 范围为 0 到 65535 无符号
unsigned is used when ur value must be positive, no negative value here,
if signed for int range -32768 to +32767
if unsigned for int range 0 to 65535
无符号变量是内部表示的变量,没有数学符号(加号或减号)只能存储“零”或正值。 假设无符号变量的大小为n 位,那么它可以表示 2^n(2 n 次方)值 - 0 到 (2^n -1)。 另一方面,有符号变量会“丢失”一位来表示符号,因此它可以存储从 -(2^(n-1) -1) 到 (2^(n-1)) 的值(包括零)。 因此,有符号变量可以存储正值、负值和零。
PS:
在内部,数学符号可以用一个补码形式、两个补码形式或符号位来表示(例如:0 -> +、1-> -)
所有这些方法有效地将 n 位 (2^n) 中可表示值的范围划分为三个部分:正、负和零。
这只是我的两分钱。
我希望这有帮助。
Unsigned variables are variables which are internally represented without a mathematical sign (plus or minus) can store 'zero' or positive values only. Let us say the unsigned variable is n bits in size, then it can represent 2^n (2 power n) values - 0 through (2^n -1). A signed variable on the other hand, 'loses' one bit for representing the sign, so it can store values from -(2^(n-1) -1) through (2^(n-1)) including zero. Thus, a signed variable can store positive values, negative values and zero.
P.S.:
Internally, the mathematical sign may be represented in one's complement form, two's complement form or with a sign bit (eg: 0 -> +, 1-> -)
All these methods effectively divide the range of representable values in n bits (2^n) into three parts, positive, negative and zero.
This is just my two cents worth.
I hope this helps.