Диапазон типов данных со знаком и без знака

275
user341814

Итак, если charэто, 1 byteследовательно, это 8 бит, верно?

Таким образом, 2 ^ 8 = 256 и от 0 до 255 - это диапазон char?

Как это работает с подписанными и неподписанными int? Значение int составляет 4 байта, поэтому 32 бита, так что 2 ^ 32. 2 ^ 31 - 1 дает положительный диапазон чисел со знаком, так что происходит с 32-м битом? Это используется для знака? Как знак будет храниться в памяти?

0
«Значит, если char равен 1 байту, значит, это 8 бит, верно?» Только если байт равен 8 битам. На этот счет нет законов, и компьютеры были созданы с размерами символов 6, 8, 10 и 12 бит. Аналогично, int (в C) может быть любой шириной 16 бит или шире. Что касается знака, Google "два дополнения". Daniel R Hicks 9 лет назад 0

1 ответ на вопрос

0
Kenneth L

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.

Спасибо за ** обширный ** ответ. Теперь я должен посмотреть комплимент двух, подписанное представление величины ... user341814 9 лет назад 0

Похожие вопросы