Can negating a non-negative signed integer value ever overflow?

A

Alex Fraser

Can negating a non-negative signed integer value ever overflow? Put another
way, can it be true that (mathematically) INT_MIN > -INT_MAX, LONG_MIN
-LONG_MAX etc?

I know that typically it can't overflow, but if that isn't guaranteed, how
can I portably detect if it would?

Alex
 
J

Jordan Abel

Can negating a non-negative signed integer value ever overflow? Put another
way, can it be true that (mathematically) INT_MIN > -INT_MAX, LONG_MIN

I know that typically it can't overflow, but if that isn't guaranteed, how
can I portably detect if it would?

Alex

C99 only allows twos-complement, ones-complement, and signed-magnitude
representation. It is unclear what C89 allows.
 
A

Alex Fraser

Jordan Abel said:
C99 only allows twos-complement, ones-complement, and signed-magnitude
representation.

Which implies INT_MIN is either -INT_MAX (one's complement or sign and
magnitude) or -INT_MAX - 1 (two's complement), etc, so that's OK.
It is unclear what C89 allows.

Hmm. A shame.

I know it normally can't overflow, and I can't see how to detect if it
could/would overflow anyway, so I guess should shrug my shoulders and move
on :).

Thanks,
Alex
 
J

Joe Wright

pete said:
No context? Chuck is gonna getcha.

Assume 2's complement.

int i = INT_MIN;

This is -2147483648 and 0x80000000. If then..

i = -i;

...the result is the same. Did anything happen? Was it overflow?
 
R

Richard Heathfield

Joe Wright said:
No context? Chuck is gonna getcha.

Assume 2's complement.

int i = INT_MIN;

This is -2147483648 and 0x80000000. If then..

i = -i;

..the result is the same. Did anything happen? Was it overflow?

This may have escaped your notice, I know - but -2147483648 does not quite
qualify as non-negative, so it falls outside the domain of the question.
 
K

Keith Thompson

Joe Wright said:
No context? Chuck is gonna getcha.

What do you mean? pete quoted the relevant portion of the parent
article, including the attribution line, and answered the question.
(And Richard already pointed out your other error.)
 
C

CBFalconer

Keith said:
What do you mean? pete quoted the relevant portion of the parent
article, including the attribution line, and answered the question.
(And Richard already pointed out your other error.)

Yes, I saw no reason to unleash the slavering dogs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
J

Joe Wright

CBFalconer said:
Yes, I saw no reason to unleash the slavering dogs.
Sorry pete. Thanks Richard, Keith and Chuck for catching me up. I read
pete's one liner as..
"Can negating a negative signed integer value ever overflow?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top