One of our teachers said that there is just one example that there is a difference between (-(a*b))
and ((-a)*b)
. He said by using two’s complement you can find one.
I am trying to find this example. Can anyone please help me?
Thanks in advance
6
Two’s complement represents numbers in the range -(2**(N-1)) .. (2**(N-1))-1, where N is the number of bits.
For example, 16-bit 2’s complement represents -32768 .. 32767.
If the product of A and B is 32768, the first expression will overflow, because 32768 cannot be represented before being negated, while the second expression will do fine because it generates the negative maximal number directly.
Two’s complement has the drawback in certain computations that it truncates toward negative infinity. One’s complement truncates toward zero (or negative zero). This is important in certain applications (details are very company-proprietary to a previous employer), and I believe this is at least part of the reason Seymour Cray used one’s complement on the CDC 6600 and its follow-ons.