for array_filter, I considered an example from php official site. I found a example there
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));
}
both function return return($var & 1) and return(!($var & 1));
I can not understand properly what does it mean and specially why 1 is there in return
It's pretty simple: & is a bitwise AND operator and it works as followed:
A | B
---------------
0 | 0 -> 0
1 | 0 -> 0
0 | 1 -> 0
1 | 1 -> 1
And if you take as an example 2, 3 and 6 for odd then this is what's going on:
0000 0010 -> 2
0000 0001 -> 1
--------- &
0000 0000 = return 0
0000 0011 -> 3
0000 0001 -> 1
--------- &
0000 0001 = return 1
0000 0110 -> 6
0000 0001 -> 1
--------- &
0000 0000 = return 0
So in other words if the first digit (1) is 'on' it's going to return 1 because it's odd and if not it's going to return 0
And the same is going on for even just with a NOT (!) operator
Related
Can somebody explain how this works? Thanks a lot :)
define("FLAG_A", 1);
define("FLAG_B", 2);
define("FLAG_C", 4);
function test_flags($flags=false) {
if ($flags & FLAG_A) echo "A";
if ($flags & FLAG_B) echo "B";
if ($flags & FLAG_C) echo "C";
}
test_flags(FLAG_A|FLAG_B);
This is done using what is called bitwise math. Specifically, they are using the & (bitwise and) operation to check to see if a value is set. For the sake of argument here, we will use 4-bit numbers, to keep it short and simple.
The number 7, in 4-bit binary, is as follows:
0111
Each digit has a value that is doubled each time, and all of them are added together to make the total number of 7. Broken down, it works like so:
0 1 1 1
^ ^ ^ ^
| | | |
8 4 2 1
So, essentially, it is:
0 * 8 + 1 * 4 + 1 * 2 + 1 * 1 = 7
Now, with bitwise math, specifically the bitwise and, we can say that we only care about bits in certain columns -- basically, the bit in each column must be 1, or it will be set to zero. So, checking for '4' inside '7' with bitwise math:
0111 (7)
& 0100 (4)
______
0100 (4)
Since this value is non-zero, it is true, and we can determine that the value 4 is inside the value 7. Now consider the number 11:
1 0 1 1
^ ^ ^ ^
| | | |
8 4 2 1
1 * 8 + 0 * 4 + 1 * 2 + 1 * 1 = 11
Try looking for 4 in that
1011 (11)
& 0100 (4)
______
0000 (0)
Since it has a value of 0, it is false, and we can consider that the number (4) is not inside the number 11.
Similarly, we can conclude that numbers 4, 2, and 1, but not 8, are in 7. In 11, we have 8, 2, and 1. By treating numbers as a series of bits, instead of a singular value, we can store many flags inside a single integer.
Definitely do some more reading on bitwise math, it can be very useful when used correctly, just don't try to shoe-horn it into everything.
This is based on bitwise operators. Think of
define("FLAG_A", 0001); // in binary form
define("FLAG_B", 0010);
define("FLAG_C", 0100);
Hence, e.g., $flags & FLAG_A is a bitwise AND between the variable $flags and the constant FLAG_A.
I've stumbled upon something odd, and I can't find any answers anywhere. &= seems to interpret even numbers as false. Is there a logical explanation for this, or is this a bug?
This snippet reproduces the problem, at least on my setup:
$nums = array(1,2,3,4,5,6,7,8,9,10);
$var1 = true;
$var2 = true;
foreach ($nums as $num) {
// Test
$var1 &= $num;
$var2 = $var2 && $num;
echo "$var1, $var2<br />";
//Reset
$var1 = true;
$var2 = true;
}
System: PHP Version 5.3.10-1ubuntu3.4
If I understand your question correctly, you are confusing Bitwise AND (&) with Logical AND (&&). No, they are not the same.
Bitwise operations are best understood if you inspect the binary representation of numbers. Here is what happens with even/odd numbers:
/* 1 & 0 */ 00000001b & 00000000b // 00000000b
/* 1 & 1 */ 00000001b & 00000001b // 00000001b
/* 1 & 2 */ 00000001b & 00000010b // 00000000b
/* 1 & 3 */ 00000001b & 00000011b // 00000001b
For logical operations, you simply need to look at the truthiness of operands:
1 && 0 // false -- 0 is falsy
1 && 1 // true -- any non-zero number is truthy
1 && 2 // true
1 && 3 // true
You are doing a bitwise AND on the numbers.
The numbers in binary are...
1 => 0001
2 => 0010
3 => 0011
4 => 0100
5 => 0101
6 => 0110
7 => 0111
8 => 1000
9 => 1001
10 => 1010
Realise that when you increment the number, the least significant digit is always changing, and since you're ANDing with 1, it will look like the even numbers are false as the result is 0 (0000 & 1 is 0).
I can't understand how the following code works.
$start = 1;
while($start<10){
if ($start&1) {
echo "ODD ".$start." <br/> ";
}
else {
echo "EVEN ".$start." <br/> ";
}
$start++;
}
The $start&1 will return ODD and EVEN seperately.
Output
ODD 1
EVEN 2
ODD 3
EVEN 4
ODD 5
EVEN 6
ODD 7
EVEN 8
ODD 9
If we give $start&2 instead of $start&1, it returns with another order.
How &1 &2 etc... works here?
It is a bitwise and operator.
0001 --> 1
& 0001
----
0001 --> 1
0010 --> 2
& 0001
----
0000 --> 0
0011 --> 3
& 0001
----
0001 --> 1
Depending on the endianness, it will be either the leftmost or rightmost bit that matters in this check. The above is &ing with 1. In your second example, &ing with 2, that would be
0001 --> 1
& 0010
----
0000 --> 0
0010 --> 2
& 0010
----
0010 --> 2
0011 --> 3
& 0010
----
0010 --> 2
And for further comparison, here is 1-3 &ing with 3
0001 --> 1
& 0011
----
0001 --> 1
0010 --> 2
& 0011
----
0010 --> 2
0011 --> 3
& 0011
----
0011 --> 3
To see what is going on, follow the columns of the two numbers down. If they are both a 1 then the result has the bit set in that position to a 1. If either are a 0 then the result is a 0 in that position. So for 2 & 3..
0010 --> 2
& 0011
----
0010
||||
|||+- 0 and 1, so 0
||+-- 1 and 1, so 1
++--- 0 and 0, so 0
0010 == 2
This code is based on the & operator (the AND bitwise operator):
$start&1 will return true if the rightmost bit is 1 => the number is odd
For example, the binary representation of 5 (odd) is:
101
and 1:
001
So 101 & 001 will return 001 which is also true in PHP.
The bitwise operator & deals with bits. On computers numbers are stored as binary data (1's and 0's, also called "bits"). Here is a table of the numbers that are used in the loop.
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
Unlike decimal numbers, binary numbers are read right to left. When you look at the table you can see that even and odd binary numbers end in 0 and 1, respectively. Binary numbers that end in 0 are even, and those that end in 1 are odd.
What the & bitwise operator does is basically chip off the last digit from the binary representation of the decimal, and then returns it.
If we take the number 5 (0101) then we can set it up in pseudocode:
if last bit in 0101 is 1 then
number is odd
otherwise
number is even
As for the PHP code the expression ($start & 1) returns either 1 or 0. In PHP type juggling turns 1 into true, and 0 into false.
It loops 9 times, each time it bitwise ands with 1 (that is it ands with the second binary digit to see if it is a mutliple of 2) and prints accordingly
& is the bitwise and operator. In the expression, it's looking for births the two values share in common.
Here's a tutorial of PHP bitwise operator usage: http://www.w3resource.com/php/operators/bitwise-operators.php
Can anybody help me to understand, how the following code works ? I know it will return 1 if for odd number and 0 for even number.
echo (7 & 1); // result 1
echo (6 & 1); // result 0
I think the numbers are converted to its binary. Please correct if I'm incorrect.
7 = 0000111b
1 = 0000001b
------------
& = 0000001b = 1
And for 6:
6 = 0000110b
1 = 0000001b
------------
& = 0000000b = 0
Yes, you are performing a AND operation on the numbers, so
Dec BINARY Output
7 === 0111
1 === 0001
------------------------
AND op 0001 1
Dec BINARY
6 === 0110
1 === 0001
------------------------
AND op 0000 0
Like Wise,
Dec BINARY
7 === 0111
6 === 0110
------------------------
AND op 0110 6
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Understanding PHP's & operator
I was just looking at array_filter() function doc and they had the following code to return odd numbers...
<?php
function odd($var)
{
// returns whether the input integer is odd
return($var & 1);
}
?>
Why does $var & 1 return odd number? how does that work?
& is bitwise and. It acts as a mask on the bits of $var. All odd numbers end with 1
no bit &1
1 001 1
2 010 0
3 011 1
4 100 0
5 101 1
6 110 0
7 111 1
You are using a bitwise function with always returns 1 when anded with an odd number.
A few examples:
11 = 3
01 = 1
----
01 = odd -- return 1 (true)
100 = 4
01 = 1
-----
000 = even -- return 0 (false)
One more:
10101 = 21
01 = 1
-------
00001 = odd -- return 1 (true)
That function return 1 if var is an odd number, 0 otherwise. "&" is the AND binary operator, so it considers the last binary digit of a number.
For example:
5 in binary is 101 -> 101 & 1 = 1 -> odd number.
8 in binary is 1000 -> 1000 & 1 = 0 -> even number.