473,394 Members | 1,778 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,394 software developers and data experts.

Detecting overflow

Can anyone suggest example code, or how to proceed, in adding or
subtracting two long long integers (I'm working in gcc with Linux,
where a long long is 64 bits) and determining whether an overflow or
underflow occurs, invalidating the result?

I have written a solution in assembler. It's simple, thanks to the
overflow flag. But without access to this part of the hardware in the
C language, how would one do it?

Thank you for your help!
Nov 14 '05 #1
2 6042
On Sun, 09 May 2004 19:47:07 -0400, Paul Emmons <pe*****@voicenet.com>
wrote in comp.lang.c:
Can anyone suggest example code, or how to proceed, in adding or
subtracting two long long integers (I'm working in gcc with Linux,
where a long long is 64 bits) and determining whether an overflow or
underflow occurs, invalidating the result?
That depends on whether you are talking about signed or unsigned long
longs.
I have written a solution in assembler. It's simple, thanks to the
overflow flag. But without access to this part of the hardware in the
C language, how would one do it?

Thank you for your help!


There is no standard C way to detect overflow in signed integer types
or floating point types after it happens. The overflow itself
produces undefined behavior in these types, and the C standard no
longer places any requirements on the program.

Instead you need to check the values before hand. With the exception
of LLONG_MIN, it is not that hard. For addition there is no
possibility of an out-of-range answer if the signs of both values are
the different, for subtraction if they are the same.

If you are using unsigned long long, on the other hand, the result of
overflow or underflow is well-defined. The result is the true
arithmetic result modulo (ULLONG_MAX + 1).

So given unsigned long long values a and b, an overflow or underflow
has occurred if either:

a + b is less than a

a - b is greater than a

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
What you say makes sense. Thank you!

Nov 14 '05 #3

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

Similar topics

6
by: Csaba2000 | last post by:
How do I detect when the font size has been changed (especially by user action: either Ctrl+Scroll wheel or View/Text Size)? This is just for use on IE 5.5+, but it would be great if there was a...
3
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
7
by: wij | last post by:
Hi: Is there better way of detecting multiplication overflow for type long than by using double precision of lldiv to verify the result? Thanks in advance. I.J.Wang
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
10
by: vashwath | last post by:
Hi all, Is there any free tool available for detecting array overflow? I found one which detects overflow of dynamic arrays. But I need a tool(or a special compiler) which detects static array...
7
by: pocmatos | last post by:
Hi all, What the best way to detect under/over flow in a program with a lot of computations? For example: #include <iostream> #include <limits> using namespace std;
8
by: Ole Nielsby | last post by:
Given two longs: long a = ...; long b = ...; What's the simplest/fastest way of performing a simple operation on longs and detecting overflow? long c = a + b; // but branch on overflow
6
by: Andre Majorel | last post by:
How do you compute an off_t with overflow detection ? Ideally, the target language is C89/C90 and the target platform is reasonably recent versions of the major Unixen. If there is no practical...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.