6

I am puzzled on this code snippet:

#include <climits>
#include <iostream>
int main(void) {
    using namespace std;
    cout << "long max " << LONG_MAX << endl;
    long x = 2 * 1024 * 1024 * 1024;
    cout << "2 * 1024 * 1024 * 1024 = " << x << endl;
    return 0;
}

I was expecting 2147483648 as it should be, instead I am getting. Using unsigned doesn't seem to help. what gives?

long max 9223372036854775807
2 * 1024 * 1024 * 1024 = -2147483648
Oliver
  • 3,592
  • 8
  • 34
  • 37
  • if you type `1024`, it already has a data type, in this language being integer. That is why you need to specify `1024L` if you want long datatype. – eis Mar 26 '13 at 22:55

1 Answers1

10

Add some Ls*. long x = 2L * 1024L * 1024L * 1024L;

(Technically, as long as one literal is of type long the others will be promoted to long)

The overflow happens because 2, etc. is of type int by default and the overflow happens before the assignment.

See integer literals which explains the different literals.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • 1
    And the signed int has a maximum of `(2^31) - 1`, so `2^31` overflows that by one. (just to make it more complete). – scones Mar 26 '13 at 22:59
  • I mistakenly thought the upcast will take care of it, apparently not. thx – Oliver Mar 26 '13 at 23:00
  • @Oliver: The overflow happens before the assignment. Also, note that signed integer overflow is undefined behavior in C++. – Jesse Good Mar 26 '13 at 23:05
  • `long` is still only a 32-bit type on some systems; use `LL`. Also, the *first* or *second* literal has to be `long`, promotion happens during evaluation of each `*`. On a system with 16-bit `int`, `2 * 1024 * 1024 * 1024L` would have already overflowed `int` before evaluating the last `* 1024L` that forces promotion of the left hand side to `long`. – Peter Cordes Feb 25 '21 at 08:50