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

x
Solved

# Testing for exceeding the limits of a long

Posted on 2005-05-03
Medium Priority
277 Views
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.
Example:
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?
0
Question by:mcravenufo

LVL 8

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

Author Comment

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

LVL 22

Accepted Solution

ID: 13921922
You can get an extra bit out of things by using 'unsigned long' instead of long.

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

Author Comment

ID: 13921984
Thanks for the info.
0

LVL 55

Expert Comment

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

// error
}
0

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);
0

Author Comment

ID: 13922176
Great. I appreciate it.
0

## Featured Post

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.
###### Suggested Courses
Course of the Month18 days, 11 hours left to enroll