Why does long long n = 2000*2000*2000*2000; overflow?

long long int n = 2000*2000*2000*2000;    // overflow

long long int n = pow(2000,4);            // works
long long int n = 16000000000000;         // works

Why does the first one overflow (multiplying integer literal constants to assign to a long long)?

What's different about it vs. the second or third ones?


Because 2000 is an int which is usually 32-bit. Just use 2000LL.

Using LL suffix instead of ll was suggested by @AdrianMole in, now deleted, comment. Please check his answer.

By default, integer literals are of the smallest type that can hold their value but not smaller than int. 2000 can easily be stored in an int since the Standard guarantees it is effectively at least a 16-bit type.

Arithmetic operators are always called with the larger of the types present but not smaller than int:

  • char*char will be promoted to operator*(int,int)->int
  • char*int calls operator*(int,int)->int
  • long*int calls operator*(long,long)->long
  • int*int still calls operator*(int,int)->int.

Crucially, the type is not dependent on whether the result can be stored in the inferred type. Which is exactly the problem happening in your case - multiplication is done with ints but the result overflows as it is still stored as int.

C++ does not support inferring types based on their destination like Haskell does so the assignment is irrelevant.


The constants (literals) on the RHS of your first line of code are int values (not long long int). Thus, the mulitplications are performed using int arithmetic, which will overflow.

To fix this, make the constants long long using the LL suffix:

long long int n = 2000LL * 2000LL * 2000LL * 2000LL;

cppreference

In fact, as noted in the comment by Peter Cordes, the LL suffix is only actually needed on either the first (leftmost) or second constant. This is because, when multiplying types of two different ranks, the operand of lower rank is promoted to the type of the higher rank, as described here: Implicit type conversion rules in C++ operators. Furthermore, as the * (multiplication) operator has left-to-right associativity, the 'promoted' result of the first multiplication propagates that promotion to the second and third.

Thus, either of the following lines will also work without overflow:

long long int n1 = 2000LL * 2000 * 2000 * 2000;
long long int n2 = 2000 * 2000LL * 2000 * 2000;

Note: Although lowercase suffixes (as in 2000ll) are valid C++, and entirely unambiguous to the compiler, there is a general consensus that the lowercase letter, 'ell', should be avoided in long and long long integer literals, as it can easily be mistaken, by human readers, for the digit, 1. Thus, you will notice that 2000LL (uppercase suffix) has been used throughout the answers here presented.


2000*2000*2000*2000 is a multiplication of 4 int values, which returns an int value. When you assign this int value to long long int n the overflow already happend (if int is 32 bit the resulting value won't fit).

You need to make sure that the overflow does not occur, so when you write

long long int n = static_cast<long long int>(2000)*2000*2000*2000;

you make sure that you are doing a long long int multiplication (long long int multiplied with int returns a long long int, so no overflow in your case).

A shorter (and better way) is to write 2000LL or 2000ll instead of the static_cast. That gives the integer literal the right type. This is not needed for 2000 which fits into an int but it would be needed for higher values that don't fit into an int.

long long int n = 2000LL*2000*2000*2000;
long long int n = 2000LL*2000LL*2000LL*2000LL;