473,225 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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

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
Mar 6 '06 #1
8 2429
On 2006-03-06, Alex Fraser <me@privacy.net> 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.
Mar 6 '06 #2
Alex Fraser wrote:

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


No.

--
pete
Mar 6 '06 #3
"Jordan Abel" <ra*******@gmail.com> wrote in message
news:sl***********************@random.yi.org...
On 2006-03-06, Alex Fraser <me@privacy.net> 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
Mar 7 '06 #4
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 ---
Mar 7 '06 #5
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)
Mar 7 '06 #6
Joe Wright <jo********@comcast.net> 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) ks***@mib.org <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.
Mar 7 '06 #7
Keith Thompson wrote:
Joe Wright <jo********@comcast.net> 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/>
Mar 8 '06 #8
CBFalconer wrote:
Keith Thompson wrote:
Joe Wright <jo********@comcast.net> 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 ---
Mar 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.