我有以下代码:
print "\n1 & 11\n";
var_dump(1 & 11);
print "\n 2 & 222\n";
var_dump(2 & 222);
为什么第一个结果是 1 ?为什么第二个结果是2?
PHP Web site说 2 & 222
(例如)应该返回一个 bool 值:
For example, $a & $b == true evaluates the equivalency then the bitwise and; while ($a & $b) == true evaluates the bitwise and then the equivalency."
我不明白,2 & 222
怎么会是 2
呢?
最佳答案
&
按位执行 AND
。也就是说,它对输入的所有位执行 AND
运算。
二进制:
2 = 0000000010
222 = 1011011110
2 & 222 = 0000000010 ( = 2)
不要混淆 &
和 &&
。 &
执行按位与
,而 &&
执行逻辑与
。
2 && 222 = true
2 & 222 = 2
至于1 & 11
1 = 0001
11 = 1011
1 & 11 = 0001 ( = 1)
所以,1 & 11 = 1
进一步阅读:
关于php - PHP 中的按位运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13490240/