php - PHP 中的按位运算符

标签 php bit-manipulation operator-keyword

我有以下代码:

print "\n1 & 11\n";
var_dump(1 & 11);

print "\n 2 & 222\n";
var_dump(2 & 222);

为什么第一个结果是 1 ?为什么第二个结果是2?

PHP Web site2 & 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

进一步阅读:

http://en.wikipedia.org/wiki/Binary_and#AND

http://en.wikipedia.org/wiki/AND_gate

关于php - PHP 中的按位运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13490240/

相关文章:

php - Ajax 帖子创建 pdf

php - 用于以编程方式获取客户购物车中的产品的数据库查询

bit-manipulation - 关于使用位图存储多个值的快速备忘单

algorithm - 把这个 if-then 逻辑变成一个 bool 表达式?

javascript - 在 PHP 中调用下拉列表后,ajax 不会响应

php - SELECT 语句返回第一行而不是查找的记录

sql - 我可以将一堆 boolean 列转换为 PostgreSQL 中的单个位图吗?

C# 在 C++ 中使用 & 执行按位运算

c# - c#中的声明是分配内存还是分配内存的新运算符?

javascript - JavaScript 中的短路评估 - SyntaxError : Unexpected token 'return'