The ~
operator performs a ones-complement on its argument, and it does not matter whther the argument is a signed or unsigned integer. It merely flips all the bits, so
0000 0000 0000 1010 (bin) / 10 (dec)
becomes
1111 1111 1111 0101 (bin)
(where, presumably, these numbers are 32 bits wide -- I omitted 16 more 0's and 1's.)
How will cout
display the result? It looks at the original type. For a signed integer, the most significant bit is its sign. Thus, the result is always going to be negative (because the most significant bit in 10
is 0
). To display a negative number as a positive one, you need the two's complement: inverting all bits, then add 1. For example, -1
, binary 111..111
, displays as (inverting) 000..000
then +1: 000..001
. Result: -1
.
Applying this to the one's complement of 10
you get 111..110101
-> inverting to 000...001010
, then add 1
. Result: -11.
For an unsigned number, cout
doesn't do this (naturally), and so you get a large number: the largest possible integer minus the original number.