title: Force PHP integer overflow
body: <p>We have some integer arithmetic which for historical reasons has to work the same on PHP as it does in a few statically typed languages.  Since we last upgraded PHP the behavior for overflowing integers has changed.  Basically we are using following formula:</p>
  
  <pre><code>function f($x1, $x2, $x3, $x4)
  {
     return (($x1 + $x2) ^ $x3) + $x4;
  }
  </code></pre>
  
  <p>However, even with conversions:</p>
  
  <pre><code>function f($x1, $x2, $x3, $x4)
  {
     return intval(intval(intval($x1 + $x2) ^ $x3) + $x4);
  }
  </code></pre>
  
  <p>I am still ending up with the completely wrong number...</p>
  
  <p>For example, with $x1 = -1580033017, $x2 = -2072974554, $x3 = -1170476976) and $x4 = -1007518822, I end up with -30512150 in PHP and 1617621783 in C#.</p>
  
  <p>Just adding together $x1 and $x2 I cannot get the right answer:</p>
  
  <p>In C# I get</p>
  
  <pre><code>(-1580033017 + -2072974554) = 641959725
  </code></pre>
  
  <p>In PHP:</p>
  
  <pre><code>intval(intval(-1580033017) + intval(-2072974554)) = -2147483648
  </code></pre>
  
  <p>which is the same as:</p>
  
  <pre><code>intval(-1580033017 + -2072974554) = -2147483648
  </code></pre>
  
  <p>I don't mind writing a "IntegerOverflowAdd" function or something, but I can't quite figure out how (-1580033017 + -2072974554) equals 641959725.  (I do recognize that it is -2147483648 + (2 * 2^31), but -2147483648 + 2^31 is -1505523923 which is greater than Int.Min so why is do you add 2*2^31 and not 2^31?)</p>
  
  <p>Any help would be appreciated...</p>