Originally posted by Valentin Crettaz:

And in binary ~1 equals -2.

Hi Valentin

But I think

~1 in NOT equal to -2 in BINARY.

It is the

**implementation of negative number** which makes ~1 equal to -2, as neagtive numbers are stored as 2's compliment numbers.

if we have lets say 4 bit integer size then -1 will be stored as :

i) 1 in binary = 0001

ii) 2's compliment of 1 = 1111 (invert 0001, becomes 1110, add 1, becomes 1111);

so

*-1 will be stored as 1111 *(NOTE: MSB is '1'(on) which indicates that number is negative. 2's compliment is of positive number(eg. 1))

This is done for lot of reason, it makes airthmetic operation fasts(there are lot of algos for binary airthmetic), and it also makes -0 and +0 equal.

I just want to say that it is the implementation which makes ~1 to equal to -2, in binary it will be some different number.

CMIW

AND

**thanks a lot** for the general rule. It will really help us in binary questions in

SCJP.

------------------

Regards

Ravish

[This message has been edited by ravish kumar (edited November 18, 2001).]