posted 23 years ago
The reasoning behind this formula is as follows:
1) Complement of a number is basically inverting the bits i.e. 0 ->1 and vice versa.
So, when we put a '-' sign in front of a no, then internally, this is calculated in 2s complement form:- first invert the bits (i.e. 1's complement) and then add 1 to this complemented number.
2) Since, we are just intrested in the inverted bits, subtracting 1 from the above inverted number will st away give the 1s complement form of the original no.
In short,
for -2:
2 -> 0010
-2 -> in 2s complement is: 1's complement + 1 i.e
1s complement of 2 = ivert bits of 0010 = 1101
now adding 1 gives: 1110 which is the 2s complement form of -2
Hence, doing exactly the opposite of last step would give as the desired quick answer for getting ~2.