Remember
Register
All Activity
Tags
Users
Ask a Question
About Boostr
Advertise on boostr.in
Jobs
callback function return returnvar 1 using -'php,bitwise-operators'
callback function return returnvar 1 Â using -'php,bitwise-operators'
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?
Â Â Â Â
php
bitwise-operators
asked
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
4
views
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or
register
.
8 Answers
0
votes
$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).
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
& 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
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
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.
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
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...
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
& 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".
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
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)
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
& 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.
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
0
votes
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
answered
Sep 8, 2015
by
rajesh
Please
log in
or
register
to add a comment.
...