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?
-1
11111111111111111111111111111111
4294967295
It is interpreted as a signed integer, and in two additions an integer with all bits 1 is equal -1.
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 .
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.
1