In article <f9************ *************** *******@1g2000h sl.googlegroups .com>,
Chad <cd*****@gmail. comwrote:
>I really don't understand how something like..
>#define abs(x) (((x) < 0) ? -(x) : (x))
>could cause a possible overflow.
e.g., INT_MIN might be -32768.
-(-32768) ---32768 but INT_MAX might be 32767
This is an issue in any implementation that uses twos complement
(unless it reserves the minimum integral value for some reason,
which would be quite unusual.)
It might help to look at the binary values involved.
On a system with a two-byte unsigned short, for example,
SHORT_MIN would be 0x8000 . To take the negative,
toggle each bit and then add 1 to the result. Toggling each
bit of 0x8000 would be 0x7FFF; add 1 and you get 0x8000 which
is the original value back again.
--
"I was very young in those days, but I was also rather dim."
-- Christopher Priest