What is the difference between signed and unsigned variables?

I saw these mentions in the context of C and C ++, but what is the difference between signed and unsigned variables?

+47
variables language-agnostic unsigned
Mar 07 '09 at 4:11
source share
8 answers

Signed variables , such as signed integers, allow you to represent numbers in both positive and negative ranges.

Unsigned variables , such as unsigned integers, allow you to represent numbers in positive.

Unsigned and signed variables of the same type (for example, int and byte ) have the same range (range of 65,536 and 256 numbers, respectively), but unsigned can represent a larger value than the corresponding signed variable .

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

The Wikipedia page on Designated Numbers explains the difference in bit level representation and Integer (computer science) contains a range table for each integer type with / without signature.

+101
Mar 07 '09 at 4:14
source share
— -

While commonly called the "sign bit", the binary values ​​that we usually use do not have a true sign bit.

Most computers use arithmetic with two additions. Negative numbers are created by accepting one's complement (flip all the bits) and add one:

5 (decimal) -> 00000101 (binary)
1 complement: 11111010
add 1: 11111011 which is 'FB' in hex


This is why the signed byte contains 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
(adding from 1 to 127 gives :)
1 0 0 0 0 0 0 0 , which we see at the top of this diagram is -128.


If we had the correct sign bit, the range of values ​​would be the same (for example, from -127 to +127), because one bit is reserved for the sign. If the most significant bit is a sign bit, we will have:

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

Interesting in this case is zero and negative zero:
0 (decimal) -> 00000000 (binary)
-0 (decimal) -> 10000000 (binary)


We do not have -0 with two additions; which will be -0 -128 (or more general, more than the largest positive value). However, we do with one addition; all 1 bit negative 0.

Mathematically, -0 is 0. I vaguely remember the computer, where -0 <0, but now I can not find a link to it.

+37
Oct 11 2018-10-10T00:
source share

Signed variables use one bit to indicate whether they are positive or negative. Unsigned variables do not have this bit, so they can store large numbers in the same space, but only non-negative numbers, for example. 0 and above.

Read More: Unsigned and Signed Integers

+18
Mar 07 '09 at 4:12
source share

Unsigned variables can only be positive numbers because they have no way of indicating that they are negative.

This ability is called a “sign” or “signature bit”.

A side effect is that without a signature bit, they have one more bit that can be used to represent a number, doubling the maximum number that it can represent.

+3
Mar 07 '09 at 4:15
source share

Signed variables can be 0, positive or negative.

Unsigned variables can be 0 or positive.

Unsigned variables are sometimes used because more bits can be used to represent the actual value. Providing you with a greater range. You can also make sure that a negative value will not be passed to your function, for example.

+2
Mar 07 '09 at 4:54
source share

unsigned is used when ur should be positive, there is no negative value if signed for int range -32768 - +32767 if unsigned for int range from 0 to 65535

+2
Jul 15 '10 at 17:52
source share

Unsigned variables are variables that are represented internally without a mathematical sign (plus or minus). can only store "zero" or only positive values . Say an unsigned variable has a size of n bits , then it can represent 2 ^ n (2 powers of n) values ​​from 0 to (2 ^ n -1). On the other hand, the signed variable "loses" one bit to represent the character, so it can store values ​​from - (2 ^ (n-1) -1) to (2 ^ (n-1)), including zero. Thus, a signed variable can store positive values, negative values, and zero .

PS :.
Inside, a mathematical sign can be represented as one complement, two forms of complement or a sign bit (for example: 0 → +, 1-> -)
All these methods effectively divide the range of represented values ​​in n bits (2 ^ n) into three parts: positive, negative and zero.

These are just my two cents.

Hope this helps.

0
Mar 07 '09 at 4:58
source share

This may not be an exact definition, but I will give you an example: If you were to create a random number taking it from the system time, using an unsigned variable here is useful, since there is a large area for random numbers, since signed numbers give as positive, so are negative numbers. Since system time cannot be negative, we use an unsigned variable (only positive numbers), and we have a wider range of random numbers.

-one
Jun 22 '16 at 11:12
source share



All Articles