理解 PHP & (&, bitwise and) 运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/600202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding PHP & (ampersand, bitwise and) operator
提问by ryonlife
I often use ($var & 1)in my code, which returns true if $varis an odd number and false if it's an even number.
我经常($var & 1)在我的代码中使用,如果$var是奇数则返回真,如果是偶数则返回假。
But what does "&" actually do?
但是“&”实际上是做什么的?
采纳答案by Marius
& is binary and. If you have a binary value, and you andwith another binary value, then the result will be the bitwise andof the two. An example:
& 是二进制的and。如果您有一个二进制值,而您and有另一个二进制值,那么结果将是两者的按位and。一个例子:
01101010
& 01011001
= 01001000
The rightmost bit is either a 1 (and in that case the number is an odd number) or it is a 0, in which case the number is even. If you &a number with 1, you only look at the least significant bit, and the if checks if the number is a 1 or a 0. As others have mentioned, look at the bitwise operators for info on how they work.
最右边的位要么是 1(在这种情况下数字是奇数),要么是 0,在这种情况下数字是偶数。如果您&的数字为 1,则您只查看最低有效位,然后 if 检查该数字是 1 还是 0。正如其他人提到的,查看按位运算符以获取有关它们如何工作的信息。
回答by thomasrutter
Two operations which are fundamental to binary systems are OR and AND.
二进制系统的两个基本运算是 OR 和 AND。
OR means 'if either A is on or B is on'. A real world example would be two switches in parallel. If either is allowing current through, then current passes through.
OR 表示“如果 A 开启或 B 开启”。一个真实的例子是两个并行的开关。如果任一允许电流通过,则电流通过。
AND means 'if both A and B is on'. The real world example is two switches in series. Current will only pass through if both are allowing current through.
AND 表示“如果 A 和 B 都打开”。现实世界的例子是两个串联的开关。只有当两者都允许电流通过时,电流才会通过。
In a computer, these aren't physical switches but semiconductors, and their functionality are called logic gates. They do the same sorts of things as the switches - react to current or no current.
在计算机中,这些不是物理开关而是半导体,它们的功能称为逻辑门。它们做与开关相同的事情——对电流或无电流做出反应。
When applied to integers, every bit in one number is combined with every bit in the other number. So to understand the bitwise operators OR and AND, you need to convert the numbers to binary, then do the OR or AND operation on every pair of matching bits.
当应用于整数时,一个数中的每一位都与另一个数中的每一位相结合。因此,要了解按位运算符 OR 和 AND,您需要将数字转换为二进制,然后对每一对匹配位进行 OR 或 AND 运算。
That is why:
这就是为什么:
00011011 (odd number)
AND
00000001 (& 1)
==
00000001 (results in 1)
Whereas
然而
00011010 (even number)
AND
00000001 (& 1)
==
00000000 (results in 0)
The (& 1) operation therefore compares the right-most bit to 1 using AND logic. All the other bits are effectively ignored because anything AND nothing is nothing. An even number in binary is also an even number in decimal notation (10 is a multiple of 2).
因此 (& 1) 操作使用 AND 逻辑将最右边的位与 1 进行比较。所有其他位都被有效地忽略,因为什么都没有。二进制中的偶数也是十进制中的偶数(10 是 2 的倍数)。
Other fundamental operations to binary systems include NOT and XOR. NOT means 'if A is off' and is the only form of logic gate that takes only one signal or 'parameter' instead of two. XOR means 'if either A or B is on, but not both'. And then there are NAND, NOR, and NXOR, which are basically just NOT combined with AND, OR, and XOR, ie NAND means 'if A and B are notboth on'.
二进制系统的其他基本操作包括 NOT 和 XOR。NOT 表示“如果 A 关闭”,并且是仅采用一个信号或“参数”而不是两个的逻辑门的唯一形式。XOR 的意思是“如果 A 或 B 打开,但不能同时打开”。然后是 NAND、NOR 和 NXOR,它们基本上只是不与 AND、OR 和 XOR 组合,即 NAND 的意思是“如果 A 和 B未同时打开”。
In programming, the operator
在编程中,操作员
& means AND,
| means OR,
~ means NOT, and
^ means XOR.
The others can be made up by combining these, for example:
其他的可以通过组合这些来组成,例如:
~ (a & b) is equivalent to a NAND operation
PHP specific note
PHP 特定说明
Bitwise operators do not work on floating-point values, and in PHP float values will be implicitly converted to integers first. Numbers outside the range that can be expressed as integers will be truncated to zero - that is, all numbers over PHP_INT_MAX will look "even" in the expression ($num & 1)). If you want to support numbers outside of PHP_INT_MIN/PHP_INT_MAX, you'll need fmod($num, 2). If, however, you're on 64-bit PHP your integers will have greater precision than floats anyway.
按位运算符不适用于浮点值,在 PHP 中浮点值将首先隐式转换为整数。超出可以表示为整数的范围的数字将被截断为零 - 也就是说,PHP_INT_MAX 上的所有数字在表达式中看起来都是“偶数” ($num & 1))。如果你想支持 PHP_INT_MIN/PHP_INT_MAX 之外的数字,你需要fmod($num, 2). 但是,如果您使用的是 64 位 PHP,您的整数无论如何都会比浮点数具有更高的精度。
回答by ólafur Waage
This is also interesting to know about bitwise and PHP:
了解按位和 PHP 也很有趣:
/**
* Regular
*/
echo (true && true); // 1
echo (true && false); // nothing
echo (true || false); // 1
echo (false || false); // nothing
echo (true xor false); // 1
echo (false xor false); // nothing
/**
* Bitwise
*/
echo (true & true); // 1
echo (true & false); // 0
echo (true | false); // 1
echo (false | false); // 0
echo (true ^ false); // 1
echo (false ^ false); // 0
回答by Pat
I know your question is about understanding the bitwise operator and the accepted answer explains it well. But for the example you give, I cannot help but recommending you use the modulo operator instead:
我知道您的问题是关于理解按位运算符,并且接受的答案很好地解释了它。但是对于您给出的示例,我不禁建议您改用模运算符:
($var % 2) /* instead of */ ($var & 1)
Because it makes the intent clear that you are checking that the number is odd (not divisible by two), and it is more generic, so you can use ($var % 3) the same way and deduce how it works for any N.
因为它清楚地表明您正在检查数字是否为奇数(不能被 2 整除),并且它更通用,所以您可以以相同的方式使用 ($var % 3) 并推断它如何适用于任何 N。
回答by mpen
In addition to the other answers, it's worth noting that
除了其他答案之外,值得注意的是
if(func1() && func2())
Will only call func2()if func1()returns true ("lazy evaluation"), whereas
仅func2()在func1()返回 true 时调用(“惰性求值”),而
if(func1() & func2())
Will call both functions regardless, but the truth tables for both will be the same (assuming they return booleans).
无论如何都会调用这两个函数,但两者的真值表将相同(假设它们返回布尔值)。
thomasrutter points out (in the comments below) that you probably shouldn't do the latter in practice. (A & B)won't necessarily have the same truthiness as (A && B), particularly when Aand Bare integers. e.g., if A=1 and B=2 (both truthy), A & B will be falsey, whereas A && B is truthy. Also, another developer may think this is a typo and 'correct' it to two ampersands.
thomasrutter 指出(在下面的评论中)你可能不应该在实践中做后者。(A & B)不一定与 具有相同的真实性(A && B),特别是当A和B是整数时。例如,如果 A=1 和 B=2(都为真),则 A & B 为假,而 A && B 为真。此外,另一位开发人员可能认为这是一个错字,并将其“更正”为两个&符号。

