Simple question about: ~

Tim Boemker boemker at hpfcdc.HP.COM
Wed Jan 27 05:07:25 AEST 1988


> Could someone please explain why the following statements both give the
> same answer?

	x= -y -1;
vs.
	x = ~y;	

On most computers, integers are stored in two's complement form.  For a
positive integer, that's the normal representation.  For a negative
integer, the two's complement is the one's complement plus one.  (Adding
one may seem curious, but it has an important advantage: there's only one
representation for zero.  In a one'c complement scheme, both a word of 1's
and a word of 0's represent zero.)

Let y be a positive integer.

-y = ~y + 1	by definition of two's complement
-y - 1 = ~y

QED.



More information about the Comp.lang.c mailing list