有符号和无符号变量之间有什么区别?

时间:2022-03-07 19:16:08

I have seen these mentioned in the context of C and C++, but what is the difference between signed and unsigned variables?

我在C和C ++的上下文中看到过这些,但有符号和无符号变量之间有什么区别?

8 个解决方案

#1


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.

无符号变量(例如无符号整数)仅允许您以正数表示数字。

Unsigned and signed variables of the same type (such as int and byte) 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.

相同类型的无符号和有符号变量(例如int和byte)都具有相同的范围(范围分别为65,536和256个数字),但无符号可以表示比相应的有符号变量更大的幅度数。

For example, an unsigned byte can represent values from 0 to 255, while signed byte can represent -128 to 127.

例如,无符号字节可以表示0到255之间的值,而带符号字节可以表示-128到127。

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.

签名数字表示的*页面解释了位级别表示的差异,整数(计算机科学)页面提供了每个有符号/无符号整数类型的范围表。

#2


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:

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
      (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:

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 0 = 2 - - - 0 1 1 1 1 1 1 0 = 126 0 1 1 1 1 1 1 = 127 (添加1到127给出:) 1 0 0 0 0 0 0 0我们在此图表的顶部看到的是-128。如果我们有一个正确的符号位,则值范围将是相同的(例如,-127到+127),因为为该符号保留了一个位。如果最重要的位是符号位,我们将:

      5 (decimal) -> 00000101 (binary)
      -5 (decimal) -> 10000101 (binary)

5(十进制) - > 00000101(二进制)-5(十进制) - > 10000101(二进制)

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.

在这种情况下有趣的是我们有零和负零:0(十进制) - > 00000000(二进制)-0(十进制) - > 10000000(二进制)我们没有-0和二进制补码;什么是-0是-128(或更一般,比最大正值多一个)。虽然我们做一个人的补充;全1位为负0。

Mathematically, -0 equals 0. I vaguely remember a computer where -0 < 0, but I can't find any reference to it now.

在数学上,-0等于0.我依稀记得一台计算机-0 <0,但我现在找不到任何参考。

#3


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.

有符号变量使用一位来标记它们是正还是负。无符号变量没有这个位,因此它们可以在同一空间中存储更大的数字,但只能存储非负数,例如0和更高。

For more: Unsigned and Signed Integers

更多信息:无符号和有符号整数

#4


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.

副作用是没有签名位,它们还有一个位可用于表示数字,它可以表示的最大数量加倍。

#5


Signed variables can be 0, positive or negative.

有符号变量可以是0,正数或负数。

Unsigned variables can be 0 or positive.

无符号变量可以是0或正数。

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.

有时使用无符号变量,因为可以使用更多位来表示实际值。给你更大的范围。另外,您可以确保不会将负值传递给您的函数。

#6


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

如果ur值必须为正,则使用unsigned,如果为int范围为0到65535,则为int范围-32768到+32767

#7


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.

无符号变量是在没有数学符号(正或负)的情况下在内部表示的变量,只能存储“零”或正值。假设无符号变量的大小为n位,则它可以表示2 ^ n(2次幂n)值 - 0到(2 ^ n -1)。另一方面,有符号变量“丢失”一位用于表示符号,因此它可以存储来自 - (2 ^(n-1)-1)到(2 ^(n-1))的值,包括零。因此,有符号变量可以存储正值,负值和零。

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.

PS:在内部,数学符号可以用一个补码形式,二进制补码形式或符号位表示(例如:0 - > +,1-> - )所有这些方法都有效地将可表示值的范围划分为n位( 2 ^ n)分为正,负和零三部分。

This is just my two cents worth.

这只是我的两分钱。

I hope this helps.

我希望这有帮助。

#8


This may not be the exact definition but I'll give you an example: If you were to create a random number taking it from the system time, here using the unsigned variable is beneficial as there is large scope for random numbers as signed numbers give both positive and negative numbers. As the system time can't be negative we use unsigned variable(Only positive numbers) and we have more wide range of random numbers.

这可能不是确切的定义,但我会给你一个例子:如果你要从系统时间创建一个随机数,这里使用无符号变量是有益的,因为随机数的范围很大,因为有符号数给出积极和消极的数字。由于系统时间不能为负,我们使用无符号变量(只有正数),我们有更广泛的随机数。

#1


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.

无符号变量(例如无符号整数)仅允许您以正数表示数字。

Unsigned and signed variables of the same type (such as int and byte) 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.

相同类型的无符号和有符号变量(例如int和byte)都具有相同的范围(范围分别为65,536和256个数字),但无符号可以表示比相应的有符号变量更大的幅度数。

For example, an unsigned byte can represent values from 0 to 255, while signed byte can represent -128 to 127.

例如,无符号字节可以表示0到255之间的值,而带符号字节可以表示-128到127。

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.

签名数字表示的*页面解释了位级别表示的差异,整数(计算机科学)页面提供了每个有符号/无符号整数类型的范围表。

#2


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:

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
      (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:

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 0 = 2 - - - 0 1 1 1 1 1 1 0 = 126 0 1 1 1 1 1 1 = 127 (添加1到127给出:) 1 0 0 0 0 0 0 0我们在此图表的顶部看到的是-128。如果我们有一个正确的符号位,则值范围将是相同的(例如,-127到+127),因为为该符号保留了一个位。如果最重要的位是符号位,我们将:

      5 (decimal) -> 00000101 (binary)
      -5 (decimal) -> 10000101 (binary)

5(十进制) - > 00000101(二进制)-5(十进制) - > 10000101(二进制)

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.

在这种情况下有趣的是我们有零和负零:0(十进制) - > 00000000(二进制)-0(十进制) - > 10000000(二进制)我们没有-0和二进制补码;什么是-0是-128(或更一般,比最大正值多一个)。虽然我们做一个人的补充;全1位为负0。

Mathematically, -0 equals 0. I vaguely remember a computer where -0 < 0, but I can't find any reference to it now.

在数学上,-0等于0.我依稀记得一台计算机-0 <0,但我现在找不到任何参考。

#3


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.

有符号变量使用一位来标记它们是正还是负。无符号变量没有这个位,因此它们可以在同一空间中存储更大的数字,但只能存储非负数,例如0和更高。

For more: Unsigned and Signed Integers

更多信息:无符号和有符号整数

#4


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.

副作用是没有签名位,它们还有一个位可用于表示数字,它可以表示的最大数量加倍。

#5


Signed variables can be 0, positive or negative.

有符号变量可以是0,正数或负数。

Unsigned variables can be 0 or positive.

无符号变量可以是0或正数。

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.

有时使用无符号变量,因为可以使用更多位来表示实际值。给你更大的范围。另外,您可以确保不会将负值传递给您的函数。

#6


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

如果ur值必须为正,则使用unsigned,如果为int范围为0到65535,则为int范围-32768到+32767

#7


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.

无符号变量是在没有数学符号(正或负)的情况下在内部表示的变量,只能存储“零”或正值。假设无符号变量的大小为n位,则它可以表示2 ^ n(2次幂n)值 - 0到(2 ^ n -1)。另一方面,有符号变量“丢失”一位用于表示符号,因此它可以存储来自 - (2 ^(n-1)-1)到(2 ^(n-1))的值,包括零。因此,有符号变量可以存储正值,负值和零。

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.

PS:在内部,数学符号可以用一个补码形式,二进制补码形式或符号位表示(例如:0 - > +,1-> - )所有这些方法都有效地将可表示值的范围划分为n位( 2 ^ n)分为正,负和零三部分。

This is just my two cents worth.

这只是我的两分钱。

I hope this helps.

我希望这有帮助。

#8


This may not be the exact definition but I'll give you an example: If you were to create a random number taking it from the system time, here using the unsigned variable is beneficial as there is large scope for random numbers as signed numbers give both positive and negative numbers. As the system time can't be negative we use unsigned variable(Only positive numbers) and we have more wide range of random numbers.

这可能不是确切的定义,但我会给你一个例子:如果你要从系统时间创建一个随机数,这里使用无符号变量是有益的,因为随机数的范围很大,因为有符号数给出积极和消极的数字。由于系统时间不能为负,我们使用无符号变量(只有正数),我们有更广泛的随机数。