PHP 等效的 javascript >>>用零填充按位运算符右移?

发布于 2024-08-29 07:01:06 字数 560 浏览 5 评论 0原文

我可以知道如何做 PHP>>> ?此类运算符在 PHP 中不可用,但在 Javascript 中可用。

我刚刚发现了一个功能,如下所示:

function zeroFill($a, $b) 
{ 
    $z = hexdec(80000000); 
        if ($z & $a) 
        { 
            $a = ($a>>1); 
            $a &= (~$z); 
            $a |= 0x40000000; 
            $a = ($a>>($b-1)); 
        } 
        else 
        { 
            $a = ($a>>$b); 
        } 
        return $a; 
}

但不幸的是,它不能完美地工作。

例如:-1149025787>>>> 0 JavaScript 返回 3145941509 PHP ZeroFill() 返回 0

May I know how can I do PHP >>> ? Such operators is not available in PHP, but is available in Javascript.

I just managed to discover a function as follow:

function zeroFill($a, $b) 
{ 
    $z = hexdec(80000000); 
        if ($z & $a) 
        { 
            $a = ($a>>1); 
            $a &= (~$z); 
            $a |= 0x40000000; 
            $a = ($a>>($b-1)); 
        } 
        else 
        { 
            $a = ($a>>$b); 
        } 
        return $a; 
}

but unfortunately, it doesn't work perfectly.

EG: -1149025787 >>> 0
Javascript returns 3145941509
PHP zeroFill() return 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

零崎曲识 2024-09-05 07:01:06
/**
 * The >>> javascript operator in php x86_64
 * Usage: -1149025787 >>> 0 ---> rrr(-1149025787, 0) === 3145941509
 * @param int $v
 * @param int $n
 * @return int
 */
function rrr($v, $n)
{
    return ($v & 0xFFFFFFFF) >> ($n & 0x1F);
}

/**
 * The >> javascript operator in php x86_64
 * @param int $v
 * @param int $n
 * @return int
 */
function rr($v, $n)
{
    return ($v & 0x80000000 ? $v | 0xFFFFFFFF00000000 : $v & 0xFFFFFFFF) >> ($n & 0x1F);
}


/**
 * The << javascript operator in php x86_64
 * @param int $v
 * @param int $n
 * @return int
 */
function ll($v, $n)
{
    return ($t = ($v & 0xFFFFFFFF) << ($n & 0x1F)) & 0x80000000 ? $t | 0xFFFFFFFF00000000 : $t & 0xFFFFFFFF;
}

好好享受。

/**
 * The >>> javascript operator in php x86_64
 * Usage: -1149025787 >>> 0 ---> rrr(-1149025787, 0) === 3145941509
 * @param int $v
 * @param int $n
 * @return int
 */
function rrr($v, $n)
{
    return ($v & 0xFFFFFFFF) >> ($n & 0x1F);
}

/**
 * The >> javascript operator in php x86_64
 * @param int $v
 * @param int $n
 * @return int
 */
function rr($v, $n)
{
    return ($v & 0x80000000 ? $v | 0xFFFFFFFF00000000 : $v & 0xFFFFFFFF) >> ($n & 0x1F);
}


/**
 * The << javascript operator in php x86_64
 * @param int $v
 * @param int $n
 * @return int
 */
function ll($v, $n)
{
    return ($t = ($v & 0xFFFFFFFF) << ($n & 0x1F)) & 0x80000000 ? $t | 0xFFFFFFFF00000000 : $t & 0xFFFFFFFF;
}

Enjoy it.

洒一地阳光 2024-09-05 07:01:06

负数的速度是使用十进制-二进制转换的两倍

function zerofill($a,$b) { 
    if($a>=0) return $a>>$b;
    if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1);
    return ((~$a)>>$b)^(0x7fffffff>>($b-1)); 

Twice as fast for negative numbers as using the decimal-binary conversions

function zerofill($a,$b) { 
    if($a>=0) return $a>>$b;
    if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1);
    return ((~$a)>>$b)^(0x7fffffff>>($b-1)); 
始终不够 2024-09-05 07:01:06

我对此进行了很多研究,从 StackOverflow 和开源项目中收集了超过 11 个版本,但没有一个起作用。但最后,我找到了解决方案。

有关更多详细信息,请查看现场演示、测试和示例,查看我的问题和答案:
PHP 中的无符号右移/零填充右移(Java/JavaScript 等效)

function unsignedRightShift($a, $b) {
    if ($b >= 32 || $b < -32) {
        $m = (int)($b/32);
        $b = $b-($m*32);
    }

    if ($b < 0) {
        $b = 32 + $b;
    }

    if ($b == 0) {
        return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1);
    }

    if ($a < 0) 
    { 
        $a = ($a >> 1); 
        $a &= 0x7fffffff; 
        $a |= 0x40000000; 
        $a = ($a >> ($b - 1)); 
    } else { 
        $a = ($a >> $b); 
    }

    return $a; 
}

I've researched a lot on this, collected more than 11 versions from StackOverflow and open-source projects, none of them worked. But finally, I found the solution.

For more details, live demo, tests and examples check my question and answer:
Unsigned Right Shift / Zero-fill Right Shift in PHP (Java/JavaScript equivalent)

function unsignedRightShift($a, $b) {
    if ($b >= 32 || $b < -32) {
        $m = (int)($b/32);
        $b = $b-($m*32);
    }

    if ($b < 0) {
        $b = 32 + $b;
    }

    if ($b == 0) {
        return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1);
    }

    if ($a < 0) 
    { 
        $a = ($a >> 1); 
        $a &= 0x7fffffff; 
        $a |= 0x40000000; 
        $a = ($a >> ($b - 1)); 
    } else { 
        $a = ($a >> $b); 
    }

    return $a; 
}
隐诗 2024-09-05 07:01:06

我在网上进行了研究,并根据给出的解释提出了我自己的零填充函数。这个方法适用于我的程序。

看看:

function zeroFill($a,$b) {
    if ($a >= 0) { 
        return bindec(decbin($a>>$b)); //simply right shift for positive number
    }

    $bin = decbin($a>>$b);

    $bin = substr($bin, $b); // zero fill on the left side

    $o = bindec($bin);
    return $o;
}

I studied around the webs and come out with my own zerofill function, base on the explanation given. This method works for my program.

Have a look:

function zeroFill($a,$b) {
    if ($a >= 0) { 
        return bindec(decbin($a>>$b)); //simply right shift for positive number
    }

    $bin = decbin($a>>$b);

    $bin = substr($bin, $b); // zero fill on the left side

    $o = bindec($bin);
    return $o;
}
荒岛晴空 2024-09-05 07:01:06

对于 32 位 (PHP_INT_SIZE == 4) 和 64 位整数 (PHP_INT_SIZE == 8):

function SHR
($x, $c)
{
    $x = intval ($x); // Because 13.5 >> 0 returns 13. We follow.

    $nmaxBits = PHP_INT_SIZE * 8;
    $c %= $nmaxBits;

    if ($c)
        return $x >> $c & ~ (-1 << $nmaxBits - $c);
    else
        return $x;
}

For both 32-bit (PHP_INT_SIZE == 4) and 64-bit integers (PHP_INT_SIZE == 8):

function SHR
($x, $c)
{
    $x = intval ($x); // Because 13.5 >> 0 returns 13. We follow.

    $nmaxBits = PHP_INT_SIZE * 8;
    $c %= $nmaxBits;

    if ($c)
        return $x >> $c & ~ (-1 << $nmaxBits - $c);
    else
        return $x;
}
梦巷 2024-09-05 07:01:06

您的函数不起作用,因为当 $b == 0 时,

$a >> -1

将计算表达式,返回 0。

假设是 32 位计算机,您可以添加一个特殊情况:

if ($z & $a) {
  if ($b == 0)
    return $a + 0x100000000;
  else {
    ...

Your function doesn't work because when $b == 0, the expression

$a >> -1

will be evaluated, which returns 0.

Assuming 32-bit machines, you can add a special case:

if ($z & $a) {
  if ($b == 0)
    return $a + 0x100000000;
  else {
    ...
执手闯天涯 2024-09-05 07:01:06

不确定这是否适用于 php,我已经设法让它与 C# 一起工作。

int a, b, result;
//Instead of 
result = a >>> b;
//I do
result = (int)((uint)a >> b);

我通过调试使用 >>> 的代码,将其与我从 javascript 转换的代码的 C# 版本进行比较,发现了这一点。在尝试使用 b = 0 时,并使用科学计算器查看 >>>>> 的不同十六进制/十进制结果 由 javascript 生成。当a为负数时,>>>实际上使aa为无符号。

不确定这是否适用于所有场景,但对于我的情况, >>> 用于 md5 哈希。能够产生类似的输出,我对结果非常满意。

希望有帮助

Not sure if this works for php, I've manage to get it to work with C#.

int a, b, result;
//Instead of 
result = a >>> b;
//I do
result = (int)((uint)a >> b);

I found that out by debugging into the code that uses >>> comparing it with C# version of the code I converted from javascript. While trying out with b = 0, and using a scientific calculator to see the different hex/dec result of >> and >>> produce by javascript. When a is negative, >>> actually makes aa as unsigned.

Not sure if that works for all scenario, but for my case the >>> is for md5 hashing. Being able to produce similar output, I'm quite satisfied with the result.

Hope that helps

寂寞陪衬 2024-09-05 07:01:06

这对我有用

function RRR($a, $b){
    return (int)((float)$a/pow(2,(int)$b));
}

This works for me

function RRR($a, $b){
    return (int)((float)$a/pow(2,(int)$b));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文