Reputation: 923
I have the following operation in PHP:
24733 * 0x41c64e6d + 0x6073;
...and the result is: 27293242579276
With the same logic, I do the same operation in C#:
24733 * 0x41c64e6d + 0x6073;
But the result is: -1274586804
OMG, why?
Upvotes: 0
Views: 99
Reputation: 31644
In PHP here's what's happening
int(24733) *
int(1103515245) +
int(24691)
You can see this by doing a var_dump
of the values
In 64-bit builds, the maximum integer is 9223372036854775807. So PHP can support the provided answer of 27293242579276. Based on the other answer, it looks like C# just converts the numbers differently, and possibly with a lower maximum integer.
Upvotes: 0
Reputation: 11963
by default c# treats number as int
when no explicit cast is specified.
so 24733 is treated as int.
int x int + int = int
and Max int is 2147483647 which is way smaller than 27293242579276. This resulted in integer overflow.
To solve the problem, use a type with higher number of bits such as decimal
you can append a letter "m" at the end of the number to tell c# you want it to be a decimal
like 24733m * 0x41c64e6d + 0x6073
Upvotes: 2