title: Unsigned Right Shift / Zero-fill Right Shift / >>> in PHP (Java/JavaScript equivalent)
body: <p>Before flagging this as a duplicate, please read below, and check <s> <a href="http://phpfiddle.org/main/code/bcv7-bs2q" rel="nofollow noreferrer">my code</a> </s> * <a href="http://phpfiddle.org/main/code/dpkw-rxfe" rel="nofollow noreferrer">my updated code</a>!</p>
  
  <p>So my problem is that, I have to implement Java/JavaScript '>>>' (Unsigned Right Shift / Zero-fill Right Shift), but I can't get it work exactly the same way.</p>
  
  <p>I've selected the 11 most promising implementations I've found on SO and on the web (links are added as comments in the code) and added a few test cases. Unfortunately <strong>NONE</strong> of the functions returned the same response as Java/JS to ALL of the tests. <em>(Maybe some of them are only working on 32bit systems)</em></p>
  
  <p>Live Code + JS+PHP results demo (click Run): <br>
  <s><a href="http://phpfiddle.org/main/code/bcv7-bs2q" rel="nofollow noreferrer">http://phpfiddle.org/main/code/bcv7-bs2q</a></s> * <br>
  <a href="http://phpfiddle.org/main/code/dpkw-rxfe" rel="nofollow noreferrer">http://phpfiddle.org/main/code/dpkw-rxfe</a></p>
  
  <p>The closest functions are:</p>
  
  <pre><code>// http://stackoverflow.com/a/27263298
  function shr9($a,$b) { 
      if($a&gt;=0) return $a&gt;&gt;$b;
      if($b==0) return (($a&gt;&gt;1)&amp;0x7fffffff)*2+(($a&gt;&gt;$b)&amp;1);
      return ((~$a)&gt;&gt;$b)^(0x7fffffff&gt;&gt;($b-1)); 
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// http://stackoverflow.com/a/25467712
  function shr11($a, $b) { 
      if ($b &gt; 32 || $b &lt; -32) {
          $m = (int)($b/32);
          $b = $b-($m*32);
      }
  
      if ($b &lt; 0)
          $b = 32 + $b;
  
      if ($a &lt; 0) 
      { 
          $a = ($a &gt;&gt; 1); 
          $a &amp;= 2147483647; 
          $a |= 0x40000000; 
          $a = ($a &gt;&gt; ($b - 1)); 
      } else { 
          $a = ($a &gt;&gt; $b); 
      } 
      return $a; 
  }
  </code></pre>
  
  <p>Unfortunately shr9 fails on <s> (-10 >>> -3) and </s> * (32 >> 32), but is the <strong>only</strong> to pass (-3 >>> 0); and shr11 fails on (-3 >>> 0) and also (32 >>> 32).</p>
  
  <p>Test cases:</p>
  
  <pre><code>         0 &gt;&gt;&gt; 3    == 0 
           3 &gt;&gt;&gt; 0    == 3 
           0 &gt;&gt;&gt; -3   == 0 
          -3 &gt;&gt;&gt; 0    == 4294967293 (in JS); -3 (in Java)  
          10 &gt;&gt;&gt; 3    == 1 
          10 &gt;&gt;&gt; -3   == 0 
         -10 &gt;&gt;&gt; 3    == 536870910 
         -10 &gt;&gt;&gt; -3   == 7 
  -672461345 &gt;&gt;&gt; 25   == 107 
          32 &gt;&gt;&gt; 32   == 32 
         128 &gt;&gt;&gt; 128  == 128 
  </code></pre>
  
  <p>Edit: I found that <code>-3 &gt;&gt;&gt; 0</code> is equals <code>4294967293</code> only in JavaScript <em>(why?)</em>, but in Java, it equals <code>-3</code>. Unfortunately, this doesn't change the fact that I still can't get any function to pass all tests.</p>
  
  <hr>
  
  <p><strong>*BIG UPDATE:</strong> </p>
  
  <p>Since PHP 7, bit shift by a negative number is considered to be invalid and causes: "<em>Fatal error: Uncaught ArithmeticError: Bit shift by negative number</em>". According to this, I think we don't have to pass those tests, so I've updated the question and the codes.</p>