0

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?

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
Marcelo Rodovalho
  • 880
  • 1
  • 15
  • 26

2 Answers2

2

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

Steve
  • 11,696
  • 7
  • 43
  • 81
  • 2
    @Raphioly-San both are doing it right. Developers should be the one to prevent overflow so you are doing it wrong :) – Steve Feb 14 '17 at 22:09
  • Thank you @Steve! I'm a newbye in medium level languages. – Marcelo Rodovalho Feb 14 '17 at 22:10
  • Ah ok, so in your theory, they're not doing it wrong, because there is no wrong. ;) Like [this little floating point issue](http://stackoverflow.com/questions/17210787/php-float-calculation-error-when-subtracting) can be the cause of a multitude of headaches. But it's not wrong, the developer just didn't realize he should do a round() on the result... ;) – Raphioly-San Feb 14 '17 at 22:16
  • 1
    @Raphioly-San The answer explained pretty well. Computer is not able to store the entire float representation since it would require infinite amount of ram. That's why all number type got a "precision". Anything beyond the precision is not reliable. Again developers are expected to know this. – Steve Feb 14 '17 at 22:21
  • 1
    I do know about the floating point issue... I just wanted to share the example... ;) I just never realized the integer overflow issue... The only reason PHP was more correct is that it treats integers as signed 64-bit max, which resulted in the 'correct' answer... Where C#, as I assume, treats it as signed 32-bit and resulted in an unexpected answer... – Raphioly-San Feb 14 '17 at 22:30
  • @Steve How can I have the result "-1274586804" in PHP? – Marcelo Rodovalho Feb 14 '17 at 22:40
  • @Raphioly-San it is a huge waste of resource if you store a 32bit number using 64 bit or even 128 bit (decimal). But you can always use decimal instead of int if resource usage is not an issue for you – Steve Feb 14 '17 at 22:43
  • @MarceloRodovalho not sure how you can cast number to integer in PHP. never used it – Steve Feb 14 '17 at 22:43
  • @Steve I can imagine, but PHP doesn't care about resources... ;) I have no clue what PHP does 'under the hood', but there are too many other things to worry about then how integers are stored. Because I am a allround developer, I have to worry about MySQL, HTML, CSS and JavaScript as well. So while I'm interested in these things/artefacts, numero uno on my list is "get it to work"... ;) – Raphioly-San Feb 15 '17 at 09:25
0

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • If I understand correctly (correct me if I'm wrong) C# will cut-off the given number (because of the overflow issue) and treat it as a 32-bit signed integer. That would explain the negated number, because the first 16-bits have a negated value when it's a signed integer. – Raphioly-San Feb 15 '17 at 09:29
  • @Raphioly-San Makes sense. I'm not a C# person so I can't say for sure – Machavity Feb 15 '17 at 13:11