Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does signed and unsigned values mean?

Tags:

What does signed mean in C? I have this table to show:

enter image description here

This says signed char 128 to +127. 128 is also a positive integer, so how can this be something like +128 to +127? Or do 128 and +127 have different meanings? I am referring to the book Apress Beginning C.

like image 482
Hello World Avatar asked Nov 25 '12 17:11

Hello World


People also ask

What is signed and unsigned values?

The XDR standard defines signed integers as integer. A signed integer is a 32-bit datum that encodes an integer in the range [-2147483648 to 2147483647]. An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295].

What does signed value mean?

A numeric variable is signed if it can represent both positive and negative numbers, and unsigned if it can only represent non-negative numbers (zero or positive numbers).

What is the difference between a signed and an unsigned number?

Signed numbers use sign flag or can be distinguish between negative values and positive values. Whereas unsigned numbers stored only positive numbers but not negative numbers.

Why do we need signed and unsigned integer?

Unsigned can hold a larger positive value and no negative value. Unsigned uses the leading bit as a part of the value, while the signed version uses the left-most-bit to identify if the number is positive or negative. Signed integers can hold both positive and negative numbers.


2 Answers

A signed integer can represent negative numbers; unsigned cannot.

Signed integers have undefined behavior if they overflow, while unsigned integers wrap around using modulo.

Note that that table is incorrect. First off, it's missing the - signs (such as -128 to +127). Second, the standard does not guarantee that those types must fall within those ranges.

like image 163
Pubby Avatar answered Sep 20 '22 15:09

Pubby


By default, numerical values in C are signed, which means they can be both negative and positive. Unsigned values on the other hand, don't allow negative numbers.

Because it's all just about memory, in the end all the numerical values are stored in binary. A 32 bit unsigned integer can contain values from all binary 0s to all binary 1s. When it comes to 32 bit signed integer, it means one of its bits (most significant) is a flag, which marks the value to be positive or negative. So, it's the interpretation issue, which tells that value is signed.

Positive signed values are stored the same way as unsigned values, but negative numbers are stored using two's complement method.

If you want to write negative value in binary, first write positive number, next invert all the bits and last add 1. When a negative value in two's complement is added to a positive number of the same magnitude, the result will be 0.

In the example below lets deal with 8-bit numbers, because it'll be simple to inspect:

positive 95: 01011111 negative 95: 10100000 + 1 = 10100001 [positive 161]           0: 01011111 + 10100001 = 100000000                                     ^                                     |_______ as we're dealing with 8bit numbers,                                              the 8 bits which means results in 0 
like image 23
jwaliszko Avatar answered Sep 20 '22 15:09

jwaliszko