Using a tilde to get the MAX value for int

I tried to get the MAX value for int using tilde. But the conclusion is not what I expected. When I ran this:

#include <stdio.h>
#include <limits.h>
int main(){
    int a=0;
    a=~a;
    printf("\nMax value: %d",-a);
    printf("\nMax value: %d",INT_MAX);
    return 0;
}

I get the output:

Maximum value: 1

Maximum Value: 2147483647

I thought (for example) if I have 0000 in RAM (I know that the first bit shows the number pozitiv or negativ). After ~ 0000 => 1111 and after - ( 1111 ) => 0111 , that I would get a MAX value.

+5
source share
7 answers

32- . - a = 0 . ~a - 0xffffffff. 32- 0xffffffff -1. , -(-1) 1, . INT_MAX 0x7fffffff.

: "- (1111) = > 0111", . ~x+1 - :

~x + 1 = ~(0xffffffff) + 1
       = 0x00000000 + 1
       = 0x00000001
+10

, std::numeric_limits<int>::max()? .

, 32- int:

int a = 0;   // a = 0
a = ~a;      // a = 0xffffffff = -1 in any twos-comp system
a = -a;      // a = 1

, . max: numeric_limits ( INT_MAX, C-only codebase).

+3

'~' . , .

int, signed int. unsigned int , .

+1

C. . INT_MAX .. limits.h stdint.h.

+1

binary 1...1111 -1. -1 * -1 = 1! : 0...0000. MSB, , 10...0000, -0, ( 0 = -0 , ).

MSB.

0

, , . , +0, -0. .

max = (~0) >> 1;  

C/++, >>>. , .

0

2 111111... - -1; - ( ), , +1.

MSB, , . : int , , , .

0

All Articles