Arithmetic result for the same expression lead to different outcomes depending on wether I define an integer in one line or I use several steps:
int main() {
unsigned long long veryBigIntOneLine = ((255*256+255)*256+255)*256+255;
unsigned long long veryBigInt = 255;
veryBigInt *= 256;
veryBigInt += 255;
veryBigInt *= 256;
veryBigInt += 255;
veryBigInt *= 256;
veryBigInt += 255;
unsigned long long veryBigIntParanthesis = (((((255*256)+255)*256)+255)*256)+255;
unsigned long long fourthInt = 256;
fourthInt *= 256;
fourthInt *= 256;
fourthInt *= 256;
--fourthInt;
cout << "veryBigIntOneLine: " << veryBigIntOneLine << endl;
cout << "veryBigInt: " << veryBigInt << endl;
cout << "veryBigIntParanthesis: " << veryBigIntParanthesis << endl;
cout << "fourthInt: " << fourthInt << endl;
return 0;
}
they should all describe the same number, 256^4-1 (or 2^32-1), but the outcome is different.
veryBigIntOneLine: 18446744073709551615
veryBigInt: 4294967295
veryBigIntParanthesis: 18446744073709551615
fourthInt: 4294967295
4294967295 is the expected answer (as it is given for all four expressions by the Google calculator).
Also 18446744073709551615 is probably not an exact result of what is computed as I get an overflow warning at compilation time for both one line expressions (even when I tried with type __int128). It is actually 2^64-1, which is the max value for unsigned long long with my compiler (veryBigIntOneLine+1 gives 0).