# The difference between (-(a*b)) and ((-a)*b)

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

• I'm voting to close this question as off-topic because it is about mathematical logic and not about a programming concept. – user53019 Feb 15 '15 at 1:53
• Get a sheet of paper and write out some examples. Work through the order of precedence for the operations and you'll find what the teacher was getting at, or if they were wrong, or if you misunderstood what they said. – user53019 Feb 15 '15 at 1:54
• @GlenH7 But this is related to my compiler course. But maybe you are right. – TTS Feb 15 '15 at 1:54
• Operational precedence is inherited by programming from mathematics, not the other way around. Mathematics can safely claim to predate programming by more than a few years... – user53019 Feb 15 '15 at 1:56
• Mathematically, they're equal. This seems like a programming question because I think there is an example under two's complement notation they the two are different. That being said, we don't do your homework here. Ask your teacher, and if they don't answer then either they were wrong or it's homework. – raptortech97 Feb 15 '15 at 2:27

## 1 Answer

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.