Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overflow of 32 bit variable

Tags:

c

Currently I am implementing an equation (2^A)[X + Y*(2^B)] in one of my applications.

The issue is with the overflow of 32 bit value and I cannot use 64 bit data type.

Suppose when B = 3 and Y = 805306367, it overflows 32bit value, but when X = -2147483648, the result comes backs to 32 bit range.
So I want to store the result of (Y*2^B). Can anyone suggest some solution for this.... A and B are having value from -15 to 15 and X,Y can have values from 2147483647..-2147483648.
Output can range from 0...4294967295.

like image 260
latot Avatar asked Jun 12 '11 12:06

latot


2 Answers

If the number is too big for a 32 bit variable, then you either use more bits (either by storing in a bigger variable, or using multiple variables) or you give up precision and store it in a float. Since Y can be MAX_INT, by definition you can't multiply it by a number greater than 1 and still have it fit in a 32 bit int.

like image 192
Paul Tomblin Avatar answered Sep 28 '22 07:09

Paul Tomblin


I'd use loop, instead of multiplication, in this case. Something like this:

int newX = X;
int poweredB = ( 1 << B ); // 2^B
for( int i = 0; i < poweredB ; ++i )
{
    newX += Y; // or change X directly, if you will not need it later.
}
int result = ( 1 << A ) * newX;

But note : this will work only for some situations - only if you have the guarantee, that this result will not overflow. In your case, when Y is large positive and X is large negative number ("large" - argh, this is too subjective), this will definitely work. But if X is large positive and Y is large positive - there will be overflow again. And not only in this case, but with many others.

like image 44
Kiril Kirov Avatar answered Sep 28 '22 08:09

Kiril Kirov