Testing for exceeding the limits of a long

Posted on 2005-05-03
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
    LVL 8

    Expert Comment

    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

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

    Accepted Solution

    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)

    Author Comment

    Thanks for the info.
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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

    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

    Great. I appreciate it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now