! is a logical operator and ~ is a bitwise operator. Take the following example:
Code:
int x, y, z;
x = 7;
y = !7;
z = ~7;
! makes anything which is false (i.e 0) true (i.e. non zero) and anything which is true, false. Therefore y will become 0.
~ inverts each individual bit in the number. The number 7 is stored in an integer (4 bytes on most systems) an will be represented as follows in memory:
11100000 00000000 0000000 00000000
The ~ operator changes all the 1's to 0's and all the 0's to 1's. Changing the number to this:
00011111 11111111 11111111 11111111
As this is a signed integer the above is -8. Therefore z beomes -8.