Why is ~ 0 equal to -1?

Since I read about~ ,

Executes the NOT operator on each bit.

So I tried:

 0 = 00000000000000000000000000000000

         so ~0 should be

~0 = 11111111111111111111111111111111

But when I tried, it returns -1. Isn't 11111111111111111111111111111111it 4294967295in decimal?

+5
source share
3 answers

It is interpreted as a signed integer, and in two additions an integer with all bits 1 is equal -1.

+9
source

Only if the type is unsigned. Signed integers use the topmost bit as a negation flag - and thus setting it to 1 results in a negative number. See Two Additions .

+7
source

The bit pattern for all 1is equal 4294967295for an unsigned integer or a signed integer with more than 32 bits. For a signed integer with 32 bits, represented as two additions, it -1.

+6
source

All Articles