So are you saying that the sum will always be a negative number?
Main Topics
Browse All TopicsI 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?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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)
Business Accounts
Answer for Membership
by: mnashadkaPosted on 2005-05-03 at 14:23:09ID: 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.