1000000000 * 3 = -1294967296?

I'm confused!

Today is november 3

DateTime DateTime = new DateTime(2010,11,3); long shazbot = 1000000000 * DateTime.Day; 

shazbot goes to -1294967296

Yes???

+6
c # integer-overflow
source share
2 answers

shazbot may be long , but not 1000000000 or DateTime.Day . So, C # first performs int multiplication (which leads to overflow), and then passes it to long for storage in shazbot .

If you want to get the result of long , make one of them long , for example:

long shazbot = 1000000000L * DateTime.Day;

Edit: C # gives you a warning if you use l instead of l . Fixed.

+41
source share

Set up like this:

 long shazbot = 1000000000L * DateTime.Day; 
+1
source share

All Articles