Jason Attin wrote:
Right, so I anwered D but the correct answer is B. I applied some basic logic here and - not knowing by heart the min value of an integer - i picked a large number say -xxxxxxxxxx and stored the value in a and then did
b = -(-xxxxxxxxxx) which gives me a positive number. But, of course, I was wrong. SO the explanation goes into a long description of bits etc, stuff that I thought it wouldn't be needed for the exam. ANy quick and dirty way to figure out what it prints without going into bits?
I agree, but am not completely sure, but... yes, I highly doubt that knowledge of the Twos Complement format would be needed for the
test...
The short answer is, with the Twos Complement format, the range isn't balanced. The negative of MIN_VALUE is a positive number that is larger than MAX_VALUE, and specifically, it will overflow and become MIN_VALUE again.
If you really want to know why, then I suggest doing some research on Two Complement. If you are *not* interested, and just want a "quick and dirty" way to remember, it is simple. This is the only case where this happens -- meaning, this is the only case where negating will cause an overflow.
Henry