What is the difference between signed and unsigned variables?

后端 未结 8 994
一个人的身影
一个人的身影 2020-12-07 13:04

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

相关标签:
8条回答
  • 2020-12-07 13:41

    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

    0 讨论(0)
  • 2020-12-07 13:45

    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.

    0 讨论(0)
  • 2020-12-07 13:46

    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 讨论(0)
  • 2020-12-07 13:46

    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

    0 讨论(0)
  • 2020-12-07 13:57

    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.

    0 讨论(0)
  • 2020-12-07 14:01

    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.

    0 讨论(0)
提交回复
热议问题