Signed arithmetic with two additions

I was thinking about ranges of data types, the question is. As you know, the range under the char sign is from -128 to 127. I got it as 127, i.e. 0111111 = +127

But I could not understand how -128? if we are just a bit of the ON sign, we get 11111111, how is it equal to -128?

+5
source share
4 answers

In most cases, computers use what is called a 2 complement to represent signed integers.

Method 2 complements the fact that the possible values ​​are in a huge loop from 0 to MAX_VALUE, to MIN_VALUE, to zero, etc.

So the minimum value is the maximum value of +1 - 01111111 = 127and 10000000 = -128.

, - -2 + 1, 11111110 + 00000001 = 11111111 = -1, , .

, , , , , , 0 .

+8

, -128 - 10000000.

+2

, 1; -128 - , (.. ). -128 10000000. 1000... .

0
source

One easy way to think about this is to start with 01111111, and then continue to subtract 1 until it wraps around; the previous value is the smallest negative value. Subtracting 1 from 00000000 using the standard "borrowing" method gives 11111111, which is really a binary representation for -1. We can continue to subtract up to 10,000,000, which is -128, and again subtract another profitability of 01111111, turning around.

0
source

All Articles