[Last Call] Learn how to a build a cloud-first strategyRegister Now


Testing for exceeding the limits of a long

Posted on 2005-05-03
Medium Priority
Last Modified: 2010-04-01
I am in a situation where I can not use unsigned long or any other variable type.
I must use long.
My problem is that I ran into a situation where I may have two extremly large long values that need to be added together with the result put into another long variable.
long x = 1178610030
long y = 1115152266
x + y = ?

I think a long has a limit of 2147483647 so if I add x + y my results are not what I should expect.
I actually get a negative number. This means I can't check to see if the result is larger than the limit of a long type.

Is there an easy way to check to see if the sum is larger than the limit of a long?
Question by:mcravenufo

Expert Comment

ID: 13921856
You could do something like:
if(x > 0 && y > 0 && (x + y) < 0)
  // Error
The only way that the error could happen is if both numbers are positive and the sum is negative.  You could do bitmasking too, but this is probably more clean.

Author Comment

ID: 13921879
So are you saying that the sum will always be a negative number?
LVL 22

Accepted Solution

NovaDenizen earned 500 total points
ID: 13921922
You can get an extra bit out of things by using 'unsigned long' instead of long.

If unsigned long won't help you, then here are some basic rules for additive overflow.

Performing Z = X + Y:
x >0 and y>0:  If z < x then overflow occurred
x > 0 and y <= 0:  no overflow is possible
x <=0 and y > 0:  no overflow is possible
x <= 0 and y <= 0:  if (z > x) then overflow occurred

Performing Z = X - Y:
x > 0 and y > 0:  no overflow is possible
x > 0 and y <= 0:  if z< x then overflow occurred
x <= 0 and y > 0:  if z > x then overflow occurred
x <= 0 and y <= 0:  if z < x then overflow occurred (only possible when x = 0 and y = -2^31)
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 13921984
Thanks for the info.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13921987
You can use a 64 bit variable (like __int64) and test 31th or 32th bit.
Something like this:

__int64 x = 1178610030;
__int64 y = 1115152266;

__int64 result = x+y;
__int64 mask31 = ~((__int64) 0x7FFFFFFF);  // mask to detect if a bit past 31th is on

if (result & mask31) {
    // error
LVL 22

Expert Comment

ID: 13922124
Actually, all those rules can be implemented like this:
long x,y,z;
bool overflow;

z = x+y;
overflow = (y <= 0 ? z > x : z < x);

z = x - y;
overflow = (y <= 0 ? z < x : z > x);

Author Comment

ID: 13922176
Great. I appreciate it.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question