Firstly, I defined two bit integers, and multiply them. The answer must overflow, and I got a negative number. Then, I defined two 64-bit integers, and multiply them. The answer must overflow, and I expected to get a negative number. But, the result was zero. I don't know why this happen, could anybody give me some help?
The code is below.
#include <iostream>
int main ()
{
int x = 1e9, y = 1e9;
printf("%d\n", x * y);
int64_t a = 36028797018963968;
int64_t b = 36028797018963968;
printf("%ld\n", a * b);
return 0;
}
The result is below.
ubuntu@VM-0-2-ubuntu:~/Projects/Test$ g++ test.cpp -o a
ubuntu@VM-0-2-ubuntu:~/Projects/Test$ ./a
-1486618624
0