Reputation: 6232
I was doing advent of code day15 and as part of it I got an unexpected result. I had to basically calculate 16807 * 16807 * 65 and the value shouldn't be bigger than int.maxValue. I noticed that my result doesn't match with the expected result so when I simplified it, it basically boils down to this(unchecked wasn't used, it's just here to satisfy compiler):
int a = unchecked(16807 * 16807 * 65); //1181022001
long b = 16807L * 16807 * 65 % int.MaxValue; //1181022009
There may be something obvious I'm missing.
Upvotes: 0
Views: 84
Reputation: 203825
int.MaxValue + 1
, when cast to an integer (in an unchecked context) is int.MinValue
(-2,147,483,648). (int.MaxValue + 1L) % int.MaxValue
is 1
. So yes, they're both different, because they're both defined to do different things.
Upvotes: 2