【问题标题】:Shortening bitwise equations缩短按位方程
【发布时间】:2013-03-14 20:25:33
【问题描述】:

类似于如何缩短正负:

$x = $x + 5;

变成

$x += 5;

你能用位运算符做类似的事情吗?例如,当应用 XOR 时,以下内容是否有效?

$x = $x ^ 1;

变成

$x ^= 1;

已经测试了这个简单的脚本,它似乎可以工作,但是使用它是否正确,还是我在这里偏离了轨道?

【问题讨论】:

  • @MihaiIorga:这个词是assignment :)
  • 分配..分配:)
  • 如果你要否定这个问题,至少说明原因。

标签: php bitwise-operators xor assignment-operator


【解决方案1】:

是的,这是正确的。

来自http://www.php.net/manual/en/language.operators.assignment.php(第一条评论)

See the Arithmetic Operators page (http://www.php.net/manual/en/language.operators.arithmetic.php)
Assignment    Same as:
$a += $b      $a = $a + $b    Addition
$a -= $b      $a = $a - $b    Subtraction
$a *= $b      $a = $a * $b    Multiplication
$a /= $b      $a = $a / $b    Division
$a %= $b      $a = $a % $b    Modulus

See the String Operators page(http://www.php.net/manual/en/language.operators.string.php)
$a .= $b      $a = $a . $b       Concatenate

See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php)
$a &= $b      $a = $a & $b     Bitwise And
$a |= $b      $a = $a | $b     Bitwise Or
$a ^= $b      $a = $a ^ $b     Bitwise Xor
$a <<= $b     $a = $a << $b    Left shift
$a >>= $b     $a = $a >> $b    Right shift

【讨论】:

  • 谢谢,这正是我要找的。​​span>
【解决方案2】:

是的,在 this comment on the Assignment Operators page

中提到过
Assignment    Same as:
$a += $b     $a = $a + $b    Addition
$a -= $b     $a = $a - $b     Subtraction
$a *= $b     $a = $a * $b     Multiplication
$a /= $b     $a = $a / $b    Division
$a %= $b     $a = $a % $b    Modulus

See the String Operators page(http://www.php.net/manual/en/language.operators.string.php)
$a .= $b     $a = $a . $b       Concatenate

See the Bitwise Operators page (http://www.php.net/manual/en/language.operators.bitwise.php)
$a &= $b     $a = $a & $b     Bitwise And
$a |= $b     $a = $a | $b      Bitwise Or
$a ^= $b     $a = $a ^ $b       Bitwise Xor
$a <<= $b     $a = $a << $b     Left shift
$a >>= $b     $a = $a >> $b      Right shift

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-19
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多