two's-complement form is usually expressed as a function B2Tw, standing for "binary to two's complement", length w.

B2Tw's principle!

We can easily get some useful conclusions according to the picture above.

Example 1: 2³¹ = -2³¹

unsigned int a;
int b;

a = 2147483648;
b = (int)a;

printf("a = %u,  b = %d\n", a, b);

The output will be a = 2147483648, b = -2147483648.

Because that:

2147483648 is (1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000)₂, which turns into a signed integer will be (-1)*2³¹, whose decimal form is -2147483648.

Actually, in c, when you input -2³¹ to a int type variable i, then perform i = -i, i is still equal to -2³¹.

Example 2: 2³² - 1 = -1

unsigned int a;
int b;

a = 4294967295;
b = (int)a;

printf("a = %u,  b = %d\n", a, b);

The output will be a = 4294967295, b = -1.

Because that:

4294967295 is (1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111)₂, which turns into a signed integer will be (-1)*2³¹ + 1*2³⁰ + ... + 1*2¹ + 1*2⁰, which is the largest negative integer, in other words, -1.

That's Amazing!