note 120940 added to function.array-filter
| From: | nicolaj dot knudsen at gmail dot com | Date: | Wed, 05 Apr 2017 12:05:24 +0000 |
| Subject: | note 120940 added to function.array-filter | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-208979@lists.php.net to get a copy of this message | ||
If you like me have some trouble understanding example #1 due to the bitwise operator (&) used,
here is an explanation.
The part in question is this callback function:
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
?>
If given an integer this function returns the integer 1 if $var is odd and the integer 0 if $var is
even.
The single ampersand, &, is the bitwise AND operator. The way it works is that it takes the
binary representation of the two arguments and compare them bit for bit using AND. If $var = 45,
then since 45 in binary is 101101 the operation looks like this:
45 in binary: 101101
1 in binary: 000001
------
result: 000001
Only if the last bit in the binary representation of $var is changed to zero (meaning that the value
is even) will the result change to 000000, which is the representation of zero.
----
Server IP: 72.52.91.14
Probable Submitter: 130.225.33.167
----
Manual Page -- http://php.net/manual/en/function.array-filter.php
Edit -- https://master.php.net/note/edit/120940
Del: integrated -- https://master.php.net/note/delete/120940/integrated
Del: useless -- https://master.php.net/note/delete/120940/useless
Del: bad code -- https://master.php.net/note/delete/120940/bad+code
Del: spam -- https://master.php.net/note/delete/120940/spam
Del: non-english -- https://master.php.net/note/delete/120940/non-english
Del: in docs -- https://master.php.net/note/delete/120940/in+docs
Del: other reasons-- https://master.php.net/note/delete/120940
Reject -- https://master.php.net/note/reject/120940
Search -- https://master.php.net/manage/user-notes.php