简体   繁体   中英

PHP vs JavaScript, Bitwise Operators

I have a function in JavaScript:

function myFunc(a, b, c)
{
  return (a ^ (b | (~c)));
}

And Equivalent in PHP:

function myFunc($a, $b, $c)
{
    return ($a ^ ($b | (~$c)));
}

The result for them are not the same:

myFunc('123', '4434', '355'); // PHP = ��� (Unknown Characters)
myFunc('123', '4434', '355'); // JavaScript = -91

What is wrong here?

In PHP you need to cast manually to integer

function myFunc($a, $b, $c)
{
    return ((int)$a ^ ((int)$b | (~(int)$c)));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM