Why is -INT_MIN = INT_MIN in a signed view with two additions?

I still havenโ€™t found a reason why a junior signed negative number does not have an equivalent signed positive number? I mean a three-digit binary number for simplicity 100 is -4? but we cannot have a positive 4 in the signed format, because we cannot. He is overflowing. So, how do we know that the two 1000 additions are -4 1000 0000 -128 and so on? We have no initial positive number

+5
source share
7 answers

One way to think about the fact that the signed two-component format works by assigning each bit a power of two, and then flipping the sign of the last power of two. For example, look at -4, which is represented as 100. This means that the value

-1 x 2^2 + 0 x 2^1 + 0 x 2^0

If we want to get a positive version of this value, we would have to deny it in order to get

 1 x 2^2 - 0 x 2^1 - 0 x 2^0

Note that this value is equal to

 1 x 2^2 + 0 x 2^1 + 0 x 2^0

, 100. , , , 4 . , 100 , , , , . - , .

, n , , -1000... 00 , , , .

? , n , -2 n - 1 2 n - 1 2 n + 1 2 ^ n . , , .

, ? , . 0 - 2 n-1 - 1 base-two. , , , , -- 2 n-1 0 2 n - 1 - 1 . , .

, !

+15

-INT_MIN - undefined C.

-INT_MIN INT_MIN . gcc, , -fwrapv.

, - undefined C . , .

#define ABS(x) ((x) > 0 ? (x) : -(x)) 

void foo(int x){  
    if (ABS(x) >= 0) {
        // some code
    }
}

(gcc, icc) , , -INT_MIN undefined.

+11

. n- , .

. , , 1, , 0, . ( , , . - 0 , .

+3

, .
.


, .
, , 0110 == 6 1001 == -6. , , .

1111? , , "" (0000 = 0; 1111 = -0), .

, , . , , 0110 = 6, - 1001, - 1001 + 1 == 1010. , " ", .

- " , ". , [0 .. 2^(bits - 1)], - . , , ( ) , [-1 .. (neg) 2^(bits - 1)].


, 3- . :

BITS  VALUE
000       0
001       1
010       2
011       3

100      -4
101      -3
110      -2
111      -1

, , , , 0, .

+3

0. 0 , . , 0 , . , -128 128, 0.

+2

0. Integer [-4, -1] (, -4, -3, -2 -1) 4 , [0,3] (, , 0, 1, 2 3) 4 , 8 3- 2 3 (= 8) .

. [-n, + n] (2 * n + 1 ). , , , ( 2).

+1

, , 1000 -4 1000 0000, -128 ?

, , .

:

, , . , -4, - | -4 |, 100.

: 100 โ†’ 011 (... 11111011 , ).

: 011 โ†’ 100 (... 11111100)

, ( 1). 100 3- -4.

To go the other way, take two additional representations (100), flip bits (011) and add one (100), now you have a representation that does not contain two additions | -4 |. 1 * 2 ^ 2 + 0 * 2 ^ 1 + 0 * 2 ^ 0 = 4. Therefore, we know that the representation started with 100 is a 3-bit, two-component representation -4.

0
source

All Articles