7

I'm witnessing a strange behavior in a .net program :

Console.WriteLine(Int64.MaxValue.ToString());
// displays 9223372036854775807, which is 2^63-1, as expected

Int64 a = 256*256*256*127; // ok

Int64 a = 256*256*256*128; // compile time error : 
//"The operation overflows at compile time in checked mode"
// If i do this at runtime, I get some negative values, so the overflow indeed happens.

Why do my Int64's behaves as if they were Int32's, although Int64.MaxValue seems to confirm they're using 64 bits ?

If it's relevant, I'm using a 32 bit OS, and the target platform is set to "Any CPU"

Brann
  • 31,689
  • 32
  • 113
  • 162

2 Answers2

20

Your RHS is only using Int32 values, so the whole operation is performed using Int32 arithmetic, then the Int32 result is promoted to a long.

Change it to this:

Int64 a = 256*256*256*128L;

and all will be well.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Dang you Jon Skeet! You beat me by about 10 seconds! :) – John Kraft May 06 '09 at 13:32
  • You're not stupid, it's just a bit unintuitive at first. I was bitten by then when I expected that "double res = someInt / otherInt" does a floating point division (which it doesn't) and learned that the left hand side does not matter. – Michael Stum May 06 '09 at 13:37
  • 1
    @Michael : no, I feel stupid because I knew it. I'm pretty sure I would have spotted the problem immediately, had it happen to someone else ! It's strange how a fresh look can change everything ! In fact, the code I posted is a simplification of the real situation I encountered, in which the problem was harder to spot. Simplifying it as I did made it obvious to every eyes but mine, because my mind was biased with the "real" problem I was facing. – Brann May 06 '09 at 16:02
4

Use:

Int64 a = 256L*256L*256L*128L;

the L suffix means Int64 literal, no suffix means Int32.

What your wrote:

Int64 a = 256*256*256*128

means:

Int64 a = (Int32)256*(Int32)256*(Int32)256*(Int32)128;
Pop Catalin
  • 61,751
  • 23
  • 87
  • 115
  • the capital suffix `L` is recommended since it's more easily recognized, and in some fonts you can't even differentiate `1` and `l` – phuclv May 30 '14 at 03:12