Pourquoi ne puis-je pas définir directement une variable __int64 à -2500000000?
-
15-11-2019 - |
Question
Ce programme est écrit en VC ++ 6.0 sur une machine WindowsXP.
Si j'essaie de définir une variable __int64 à -2500000000 directement, elle est tronquée à une valeur de 32 bits et que le complément des deux est pris.
__int64 testval;
testval = -2500000000;
à ce stade TestVal est égal à 1794967293 (110 1010 1111 1101 0000 0111 0000 0000 0000 Binary).
Lorsque je réglais la variable sur 2500000000, puis multipliez par négatif, cela fonctionne:
__int64 testval;
testval = 2500000000;
testval *= -1;
La variable testval est égale -250000000000 (1001 0101 0000 0010 1111 1001 0000 0000 0000 binaire).
Des idées? Merci.
La solution
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!
Autres conseils
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;
The correct syntax is -2500000000LL. If it doesn't work, get a newer compiler.