php - what does this mean return($var & 1); -
this question has answer here:
- reference — symbol mean in php? 15 answers
- callback function return return($var & 1)? 8 answers
in php in line below return($var & 1);
mean, & mean in context?
<?php function test_odd($var) { return($var & 1); } $a1=array("a","b",2,3,4); print_r(array_filter($a1,"test_odd")); ?>
this bitwise and
.
so, if $var
odd returns 1
.
let's $var = 13
, in binary 1101
(because 13 = 2^3 + 2^2 + 2^0
).
when 1101 & 0001
0001
. can either 1
if $var
has last bit 1 (meaning it's odd) or 0
if $var
has 0 last bite, meaning $var
written sum of powers of two, without 2^0` meaning even.
Comments
Post a Comment