, Chromium V8 Mozilla Central SpiderMonkey. JS → > , , Mozilla, 20- ( 1997 ), js/src/tests/ecma/Expressions/11.7.3.js, " ". PHP .
[LiveDemo]
<?php
function ToInteger( $n ) {
$sign = ( $n < 0 ) ? -1 : 1;
if ( $n != $n ) {
return 0;
}
if ( abs( $n ) == 0 || abs( $n ) == INF ) {
return $n;
}
return intval( $sign * floor(abs($n)) );
}
function ToInt32( $n ) {
$sign = ( $n < 0 ) ? -1 : 1;
if ( abs( $n ) == 0 || abs( $n ) == INF) {
return 0;
}
$n = ($sign * floor( abs($n) )) % pow(2,32);
$n = ( $n >= pow(2,31) ) ? $n - pow(2,32) : $n;
return ( $n );
}
function ToUint32( $n ) {
$sign = ( $n < 0 ) ? -1 : 1;
if ( abs( $n ) == 0 || abs( $n ) == INF) {
return 0;
}
$n = $sign * floor( abs($n) );
$n = $n % pow(2,32);
if ( $n < 0 ){
$n += pow(2,32);
}
return ( $n );
}
function ToUint16( $n ) {
$sign = ( $n < 0 ) ? -1 : 1;
if ( abs( $n ) == 0 || abs( $n ) == INF) {
return 0;
}
$n = ( $sign * floor( abs($n) ) ) % pow(2,16);
if ($n <0) {
$n += pow(2,16);
}
return ( $n );
}
function Mask( $b, $n ) {
$b = ToUint32BitString( $b );
$b = substr( $b, strlen($b) - $n );
$b = ToUint32Decimal( $b );
return ( $b );
}
function ToUint32BitString( $n ) {
$b = "";
for ( $p = 31; $p >=0; $p-- ) {
if ( $n >= pow(2,$p) ) {
$b .= "1";
$n -= pow(2,$p);
} else {
$b .= "0";
}
}
return $b;
}
function ToInt32BitString( $n ) {
$b = "";
$sign = ( $n < 0 ) ? -1 : 1;
$b .= ( $sign == 1 ) ? "0" : "1";
for ( $p = 30; $p >=0; $p-- ) {
if ( ($sign == 1 ) ? $sign * $n >= pow(2, $p) : $sign * $n > pow(2,$p) ) {
$b .= ( $sign == 1 ) ? "1" : "0";
$n -= $sign * pow( 2, $p );
} else {
$b .= ( $sign == 1 ) ? "0" : "1";
}
}
return $b;
}
function ToInt32Decimal( $bin ) {
$r = 0;
$sign;
if ( intval($bin[0]) == 0 ) {
$sign = 1;
$r = 0;
} else {
$sign = -1;
$r = -(pow(2,31));
}
for ( $j = 0; $j < 31; $j++ ) {
$r += pow( 2, $j ) * intval($bin[31-$j]);
}
return $r;
}
function ToUint32Decimal( $bin ) {
$r = 0;
for ( $l = strlen($bin); $l < 32; $l++ ) {
$bin = "0" . $bin;
}
for ( $j = 0; $j < 32; $j++ ) {
$r += pow( 2, $j ) * intval($bin[31-$j]);
}
return $r;
}
function RShift( $s, $a ) {
$s = ToUint32BitString( $s );
for ( $z = 0; $z < $a; $z++ ) {
$s = "0" . $s;
}
$s = substr( $s, 0, strlen($s) - $a );
return ToUint32Decimal($s);
}
function UnsignedRightShift( $s, $a ) {
$s = ToUint32( $s );
$a = ToUint32( $a );
$a = Mask( $a, 5 );
return ( RShift( $s, $a ) );
}
:
UnsignedRightShift(10, 3); (= 10 → > 3)
: , "" , (4.33s 110 000 , ~ 0.04 110 000 ), , , , . - , , .