Range of signed and unsigned data types

0

So if char is 1 byte hence it is 8 bits, right?

So 2 ^ 8 = 256 and 0 to 255 is the range of char?

How does this work with signed and unsigned ints? An int is 4 bytes, so 32 bits, so 2 ^ 32. 2 ^ 31 - 1 give you the positive range of signed ints so what happens to the 32nd bit? Is it used for the sign? How would a sign be stored in memory?

user341814

Posted 2014-07-09T21:39:46.383

Reputation: 583

"So if char is 1 byte hence it is 8 bits, right?" Only if a byte is 8 bits. There's no law to that effect, and computers have been built with character sizes of 6, 8, 10, and 12 bits. Similarly, an int (in C) may be any width 16 bits or wider. As to the sign, Google "two's complement". – Daniel R Hicks – 2014-07-10T03:14:01.713

Answers

0

If the data type is defined as a signed type there are different types of representation - mainly the signed magnitude representation and two's complement representation.

For signed magnitude representation, yes, the sign bit was stored as the the most significant bit (MSB, i.e. the leftmost bit). MSB of 0 represent a positive figure while 1 represents negative figure. Example:

 7 = 00000111
-7 = 10000111

This is simple and (relatively) human readable, however integer types are usually not kept in this way for two problems:

(1) There are two representation for zero, +0 and -0. It makes it troublesome to compare figures as it creates a special case.

(2) It is not easy to do computation (as simple as adding and subtracting). Adding two positive numbers, positive number to negative number, negative number to positive number and adding two negative numbers are four different use cases. e.g. 7+6 is straight forward

          1   Carry bit
 7 = 00000111
 6 = 00000110  (Logic for add)
..   ........
13 = 00001101

While calculating 7+(-6) means the subtraction logic to be used instead

 7 = 00000111
-6 = 10000110  (Logic for subtraction)
..   ........
 1 = 00000001

Range for a 8-bit number is therefore -(2^7)+1 to 2^7-1 (i.e. -127 to +127, with two zeroes +0 and -0). Signed magnitude representation is mainly used to keep float numbers.

And that leads to the two's complement representation. Positive numbers are represented in the same way as that of signed magnitude representation. Changing sign bit takes two steps: (1) Invert all bits (change all 0 to 1 and 1 to 0) (2) Add one.

Example, to get the representation of -6 we take following steps

 6 = 00000110
 Invert all bits:  11111001
 Add one: 11111010

So -6 is represented as 11111010. With two's complement representation, you can still read the sign from the MSB; while there is only one representation for zero: 00000000.

It is easy to do computation with binary numbers in two's complement representation as well - adding is adding. Let's see again how it works to compute 7+(-6):

     1111111   Carry bit
 7 = 00000111
-6 = 11111010  (Logic for add)
..   ........
 1 = 00000001

Range for a 8-bit number is therefore -(2^7) to 2^7-1 (i.e. -128 to +127). Note that the range is different from the signed magnitude representation.

Kenneth L

Posted 2014-07-09T21:39:46.383

Reputation: 12 537

Thanks for the extensive answer. Now I have to look up two's compliment, signed magnitude representation... – user341814 – 2014-07-10T02:45:34.543