FluorescentGreen5 FluorescentGreen5 - 2 months ago 24
C++ Question

Perform 64 bit calculations in 64 bit executable

I am using MinGW64 (with the

-m64
flag) with Code::Blocks and want to know how to perform 64 bit calculations without having to using something like:

int64_t test = int64_t(2123123123) * 17;


If I use this,
2123123123
overflows:

int64_t test = 2123123123 * 17;


I have uploaded my executable to virus total which has confirmed that my executable is x64.

Answer

The default int type is still 32 bit even in 64 bit compilations for compatibility resons.

The "shortest" version I guess would be to add the ll suffix to the number

int64_t test = 2123123123ll * 17;

Another way would be to store the numbers in their own variables of type int64_t (or long long) and multiply the varaibles. usually it's rare anyway in a program to have many "magic-numbers" hard-coded into the codebase.