回调函数返回 return($var & 1)?

发布于 2024-11-11 15:15:37 字数 841 浏览 3 评论 0原文

我已经阅读了关于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

爱给你人给你 2024-11-18 15:15:37

您知道 &&AND,但您可能不知道 &按位< /em> <代码>和。

& 运算符在位级别上工作,它是逐位的。您需要考虑操作数的二进制表示形式。

例如

710 & 210 = 1112 & 0102 = 0102 = 210

例如,表达式 $var & 1用于测试最低有效位是否为10,分别为奇数或偶数。

<代码>$var & 1

010 & 110 = 0002 & 0012 = 0002 = 010 = false(偶数)

110 & 110 = 0012 & 0012 = 0012 = 110 = true  (奇数)

210 & 110 = 0102 & 0012 = 0002 = 010 = false(偶数)

310 & 110 = 0112 & 0012 = 0012 = 110 = true  (奇数)

410 & 210 = 1002 & 0012 = 0002 = 010 = false(偶数)

等等...

You know && is AND, but what you probably don't know is & is a bit-wise AND.

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.

710 & 210 = 1112 & 0102 = 0102 = 210

For instance, the expression $var & 1 is used to test if the least significant bit is 1 or 0, odd or even respectively.

$var & 1

010 & 110 = 0002 & 0012 = 0002 = 010 = false (even)

110 & 110 = 0012 & 0012 = 0012 = 110 = true  (odd)

210 & 110 = 0102 & 0012 = 0002 = 010 = false (even)

310 & 110 = 0112 & 0012 = 0012 = 110 = true  (odd)

410 & 210 = 1002 & 0012 = 0002 = 010 = false (even)

and so on...

無處可尋 2024-11-18 15:15:37
& 

它是按位运算符。它与 $var1 的相应位进行 AND

基本上它测试 $var 的最后一位以查看数字是偶数还是奇数

例如 $var 二进制 在这种情况下返回000110 和 1

000110 &
     1
------
     0

0 (false),因此数字是偶数,并且您的函数相应地返回 false

& 

it's the bitwise operator. It does the AND with the corrispondent bit of $var and 1

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

000110 &
     1
------
     0

0 (false) in this case is returned so the number is even, and your function returns false accordingly

月亮坠入山谷 2024-11-18 15:15:37

$var & 1 - 按位与
它检查 $var 是否为 ODD 值

0 & 0 = 0,
0 & 1 = 0,
1 & 0 = 0,
1 & 1 = 1 

,因此,仅当 $var 为 ODD 时,第一个回调函数才返回 TRUE,第二个 - 反之亦然(! - 逻辑 NOT)。

$var & 1 - is bitwise AND
it checks if $var is ODD value

0 & 0 = 0,
0 & 1 = 0,
1 & 0 = 0,
1 & 1 = 1 

so, first callback function returns TRUE only if $var is ODD, and second - vise versa (! - is logical NOT).

稍尽春風 2024-11-18 15:15:37

它与 $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.

巡山小妖精 2024-11-18 15:15:37

& 是按位“与”运算符。与 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".

久而酒知 2024-11-18 15:15:37

奇数的第 0 位(最低有效位)设置为 1

           v
0 = 00000000b
1 = 00000001b
2 = 00000010b
3 = 00000011b
           ^

表达式 $var & 1 在 $var 和 1 之间执行按位 AND 运算 (1 = 00000001b)。所以
该表达式将返回:

  • $var 的第 0 位设置为 1(奇数)时,
  • 返回 1 当 $var 的第 0 位设置为 0(偶数)时,返回0

An odd number has its zeroth (least significant) bit set to 1:

           v
0 = 00000000b
1 = 00000001b
2 = 00000010b
3 = 00000011b
           ^

The expression $var & 1 performs a bitwise AND operation between $var and 1 (1 = 00000001b). So
the expression will return:

  • 1 when $var has its zeroth bit set to 1 (odd number)
  • 0 when $var has its zeroth bit set to 0 (even number)
青瓷清茶倾城歌 2024-11-18 15:15:37

&是 $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.

巨坚强 2024-11-18 15:15:37

它返回 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文