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

Discussion in 'C Programming' started by Alex Fraser, Mar 6, 2006.

  1. Alex Fraser

    Alex Fraser Guest

    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
    Alex Fraser, Mar 6, 2006
    #1
    1. Advertising

  2. Alex Fraser

    Jordan Abel Guest

    On 2006-03-06, Alex Fraser <> wrote:
    > 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


    C99 only allows twos-complement, ones-complement, and signed-magnitude
    representation. It is unclear what C89 allows.
    Jordan Abel, Mar 6, 2006
    #2
    1. Advertising

  3. Alex Fraser

    pete Guest

    Alex Fraser wrote:
    >
    > Can negating a non-negative signed integer value ever overflow?


    No.

    --
    pete
    pete, Mar 6, 2006
    #3
  4. Alex Fraser

    Alex Fraser Guest

    "Jordan Abel" <> wrote in message
    news:...
    > On 2006-03-06, Alex Fraser <> wrote:
    > > 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?

    >
    > 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
    Alex Fraser, Mar 7, 2006
    #4
  5. Alex Fraser

    Joe Wright Guest

    pete wrote:
    > Alex Fraser wrote:
    >
    >>Can negating a non-negative signed integer value ever overflow?

    >
    >
    > No.
    >

    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?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Mar 7, 2006
    #5
  6. Joe Wright said:

    > pete wrote:
    >> Alex Fraser wrote:
    >>
    >>>Can negating a non-negative signed integer value ever overflow?

    >>
    >>
    >> No.
    >>

    > 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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Mar 7, 2006
    #6
  7. Joe Wright <> writes:
    > pete wrote:
    >> Alex Fraser wrote:
    >>
    >>>Can negating a non-negative signed integer value ever overflow?

    >> No.
    >>

    > 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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 7, 2006
    #7
  8. Alex Fraser

    CBFalconer Guest

    Keith Thompson wrote:
    > Joe Wright <> writes:
    >> pete wrote:
    >>> Alex Fraser wrote:
    >>>
    >>>> Can negating a non-negative signed integer value ever overflow?
    >>> No.
    >>>

    >> 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.)


    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/>
    CBFalconer, Mar 8, 2006
    #8
  9. Alex Fraser

    Joe Wright Guest

    CBFalconer wrote:
    > Keith Thompson wrote:
    >
    >>Joe Wright <> writes:
    >>
    >>>pete wrote:
    >>>
    >>>>Alex Fraser wrote:
    >>>>
    >>>>
    >>>>>Can negating a non-negative signed integer value ever overflow?
    >>>>
    >>>>No.
    >>>>
    >>>
    >>>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.)

    >
    >
    > 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?"

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Mar 8, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raj Ashar
    Replies:
    2
    Views:
    6,502
    Carl Howells
    Jul 13, 2004
  2. Eduardo Bezerra

    Negating a List Of Numbers

    Eduardo Bezerra, Apr 13, 2005, in forum: C++
    Replies:
    19
    Views:
    523
    Jean-Sebastien Samson
    Apr 20, 2005
  3. REH

    signed integer overflow

    REH, Aug 13, 2005, in forum: C Programming
    Replies:
    27
    Views:
    986
    Alexei A. Frounze
    Aug 18, 2005
  4. REH
    Replies:
    2
    Views:
    468
  5. kwest

    Negating a regular expression

    kwest, Nov 17, 2005, in forum: Perl Misc
    Replies:
    12
    Views:
    187
    Brian McCauley
    Nov 18, 2005
Loading...

Share This Page