What is the difference between signed and unsigned int

What is the difference between signed and unsigned int?

+53
c unsigned-integer
Apr 21 '11 at 5:21
source share
4 answers

As you probably know, int is stored inside binary code. Typically an int contains 32 bits, but in some environments it may contain 16 or 64 bits (or even another number, usually, but not necessarily, two at a time).

But for this example, consider 4-bit integers. Tiny but useful for illustration.

Since there are four bits in such a whole, it can take one of 16 values; 16 - from two to fourth, or 2 times 2 times 2 times 2. What are these values? The answer depends on whether it is an integer signed int or unsigned int . With an unsigned int value, the value will never be negative; no sign associated with the value. Here are 16 possible values ​​for a four-bit unsigned int :

 bits value 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15 

... and here are 16 possible values ​​of a four-bit signed int :

 bits value 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -8 1001 -7 1010 -6 1011 -5 1100 -4 1101 -3 1110 -2 1111 -1 

As you can see, for signed int most significant bit is 1 if and only if the number is negative. Therefore, for signed int s, this bit is known as the "sign bit".

+75
Apr 26 2018-11-11T00:
source share

Sometimes we know in advance that a value stored in a given integer variable will always be positive - when it is used, for example, only for counting. In this case, we can declare an unsigned variable, as in, unsigned int num student; . With this declaration, the range of valid integer values ​​(for the 32-bit compiler) will shift from the range from -2147483648 to +2147483647 to the range from 0 to 4294967295. Thus, declaring an unsigned integer almost doubles the size of the maximum possible value, which otherwise case may have.

+10
Nov 23 '11 at 11:40
source share

In terms of ordinary people, an unsigned int is an integer that cannot be negative and therefore has a higher range of positive values ​​that it can take. A signed int is an integer that can be negative, but has a lower positive range in exchange for more negative values ​​that it can take.

+8
Jun 29 '15 at 4:58
source share

int and unsigned int are two different integer types. ( int can also be called signed int or just signed ; unsigned int can also be called unsigned .)

As the names suggest, int is an unsigned int integer type, and unsigned int is an unsigned int integer type. This means that int can represent negative values, and unsigned int can only represent non-negative values.

The C language sets some requirements for the ranges of these types. The int range must be at least -32767 .. +32767 , and the unsigned int range must be at least 0 .. 65535 . This means that both types must be at least 16 bits. They are 32 bits on many systems or even 64 bits on some. int usually has an additional negative value due to the presentation of two additions used by most modern systems.

Perhaps the most important difference is the behavior of signed or unsigned arithmetic. For a signed int overflow has undefined behavior. There is no overflow for unsigned int ; any operation that gives a value outside the range of the type is wrapped around, for example, UINT_MAX + 1 == 0 .

Any integer type, whether signed or unsigned, models a subrange of an infinite number of mathematical integers. As long as you work with values ​​within the type range, everything works. When you approach the lower or upper bound of the type, you are faced with a gap and you may get unexpected results. For signed integer types, problems arise only for very large negative and positive values ​​greater than INT_MIN and INT_MAX . For unsigned integer types, problems arise with very large positive values and at zero . This can be a source of errors. For example, this is an infinite loop:

 for (unsigned int i = 10; i >= 0; i --) [ printf("%u\n", i); } 

because i always greater than or equal to zero; that the nature of unsigned types. (Inside the loop, when i is zero, i-- sets its value to UINT_MAX .)

+8
Jun 30 '15 at 15:14
source share



All Articles