Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I directly set an __int64 variable to -2500000000?

This program is written in VC++ 6.0 on a WindowsXP machine.

If I try to set an __int64 variable to -2500000000 directly, it is truncated to a 32bit value and the two's complement is taken.

__int64 testval;
testval = -2500000000;

At this point testval equals 1794967293 (110 1010 1111 1101 0000 0111 0000 0000 binary).

When I set the variable to 2500000000 and then multiply by negative one, it works:

__int64 testval;
testval = 2500000000;
testval *= -1;

The variable testval equals -2500000000 (1001 0101 0000 0010 1111 1001 0000 0000 binary).

Any ideas? Thanks.

like image 452
E.Freitas Avatar asked Feb 18 '11 20:02

E.Freitas


2 Answers

Get a newer compiler. VC6 standard compliance is VERY poor.

In VC6, try a suffix of i64, as in

__int64 toobig = -2500000000i64;

Found the documentation!

like image 192
Ben Voigt Avatar answered Oct 15 '22 12:10

Ben Voigt


The compiler is treating the constant 2500000000 as a 32-bit number. You need to explicitly tell it to treat it as a long int by appending an LL to the end of the constant. So, try instead:

testval = -2500000000LL;

Update: Since your compiler doesn't support this, and you are stuck with VC6, try instead breaking it into a value that results from the product of two 32 bit numbers as in:

testval = -250000;
testval *= 10000;
like image 30
andand Avatar answered Oct 15 '22 10:10

andand