Почему я не могу напрямую установить переменную __int64 до -2500000000?

StackOverflow https://stackoverflow.com/questions/5046452

Вопрос

Эта программа написана в VC ++ 6.0 на компьютере WindowsXP.

Если я попытаюсь установить переменную __int64 до -2500000000 напрямую, она усекается до 32-битной стоимости, а в дополнение к два.

__int64 testval;
testval = -2500000000;
.

на данный момент TestVal равен 1794967293 (110 1010 1111 1101 0000 0111 0000 0000 двоичных).

Когда я установил переменную до 2500000000, а затем умножить на отрицательный, он работает:

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

Переменная TESTVAL равен -2500000000 (1001 0101 0000 0010 1111 1001 0000 0000 двоичный).

Есть идеи? Спасибо.

Это было полезно?

Решение

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!

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top