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

1.9k views Asked by At

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.

3

There are 3 answers

4
Ben Voigt On BEST ANSWER

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!

5
andand On

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;
0
Joshua On

The correct syntax is -2500000000LL. If it doesn't work, get a newer compiler.