回调函数返回 return($var & 1)?
我已经阅读了关于 array_filter 的 PHP 手册,
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
function even($var)
{
// returns whether the input integer is even
return(!($var & 1));
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>
即使我在这里看到结果:
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
)
但我不明白这一行: return($var & 1);
谁能给我解释一下吗?
I have read the PHP Manuel about array_filter
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
function even($var)
{
// returns whether the input integer is even
return(!($var & 1));
}
$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array(6, 7, 8, 9, 10, 11, 12);
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>
Even I see the result here :
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
)
But I did not understand about this line: return($var & 1);
Could anyone explain me about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您知道
&&
是AND
,但您可能不知道&
是按位< /em> <代码>和。&
运算符在位级别上工作,它是逐位的。您需要考虑操作数的二进制表示形式。例如
例如,表达式
$var & 1
用于测试最低有效位是否为1
或0
,分别为奇数或偶数。You know
&&
isAND
, but what you probably don't know is&
is a bit-wiseAND
.The
&
operator works at a bit level, it is bit-wise. You need to think in terms of the binary representations of the operands.e.g.
For instance, the expression
$var & 1
is used to test if the least significant bit is1
or0
, odd or even respectively.它是按位运算符。它与
$var
和1
的相应位进行 AND基本上它测试 $var 的最后一位以查看数字是偶数还是奇数
例如 $var 二进制 在这种情况下返回000110 和 1
0 (false),因此数字是偶数,并且您的函数相应地返回 false
it's the bitwise operator. It does the AND with the corrispondent bit of
$var
and1
Basically it test the last bit of $var to see if the number is even or odd
Example with $var binary being 000110 and 1
0 (false) in this case is returned so the number is even, and your function returns false accordingly
$var & 1
- 按位与它检查
$var
是否为 ODD 值,因此,仅当 $var 为 ODD 时,第一个回调函数才返回 TRUE,第二个 - 反之亦然(! - 逻辑 NOT)。
$var & 1
- is bitwise ANDit checks if
$var
is ODD valueso, first callback function returns TRUE only if $var is ODD, and second - vise versa (! - is logical NOT).
它与 $var 和 1 执行按位与。由于 1 仅设置了最后一位,因此
$var &仅当 $var 中设置了最后一位时,1
才会为 true。由于偶数永远不会设置最后一位,因此如果 AND 为真,则该数字必定是奇数。It is performing a bitwise AND with $var and 1. Since 1 only has the last bit set,
$var & 1
will only be true if the last bit is set in $var. And since even numbers never have the last bit set, if the AND is true the number must be odd.&
是按位“与”运算符。与 1、3、5(和其他奇数)$var & 1
将产生“1”,0、2、4(和其他偶数)则产生“0”。&
is bitwise "and" operator. With 1, 3, 5 (and other odd numbers)$var & 1
will result in "1", with 0, 2, 4 (and other even numbers) - in "0".奇数的第 0 位(最低有效位)设置为
1
:表达式
$var & 1
在 $var 和 1 之间执行按位 AND 运算 (1 = 00000001b)。所以
该表达式将返回:
$var
的第 0 位设置为 1(奇数)时,$var
的第 0 位设置为 0(偶数)时,返回0An odd number has its zeroth (least significant) bit set to
1
:The expression
$var & 1
performs a bitwise AND operation between $var and 1 (1 = 00000001b
). Sothe expression will return:
$var
has its zeroth bit set to 1 (odd number)$var
has its zeroth bit set to 0 (even number)&是 $var 上的 按位 AND。
如果 $var 是十进制 4,则它是二进制 100. 100 & 1 是 100,因为 $var 中最右边的数字是 0 - 而 0 & 1 是 0,因此 4 是偶数。
& is a bitwise AND on $var.
If $var is a decimal 4, it's a binary 100. 100 & 1 is 100, because the right most digit is a 0 in $var - and 0 & 1 is 0, thus, 4 is even.
它返回 0 或 1,具体取决于您的 $var
如果 $var 是奇数,则 。 (1, 3, 5 ...) it $var & 1 返回 1,否则 (2, 4, 6) $var & 1 返回 0
it returns 0 or 1, depending on your $var
if $var is odd number, ex. (1, 3, 5 ...) it $var & 1 returns 1, otherwise (2, 4, 6) $var & 1 returns 0