How & works in 7 & 1 in php - 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

Related

array_php() function in php

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

&= yields false on even numbers

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).

How does this code block works?

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

How does this php code return odd numbers? [duplicate]

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.

What "|" character do? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
I googled much for finding answer but i couldn't find any.
I saw in few free source codes that they use "|" sign to combine values or something. can someone enlighten what is different between "|" and "+"?
sample:
<?php
define('PERMISSION_DENIED', 0);
define('PERMISSION_READ', 1);
define('PERMISSION_ADD', 2);
define('PERMISSION_UPDATE', 4);
define('PERMISSION_DELETE', 8);
$read_only = PERMISSION_READ;
$read_delete = PERMISSION_READ | PERMISSION_DELETE;
$full_rights = PERMISSION_DENIED | PERMISSION_READ | PERMISSION_ADD | PERMISSION_UPDATE | PERMISSION_DELETE;
$myrights = PERMISSION_READ;
$myrights |= PERMISSION_UPDATE;
?>
why not just:
<?php
define('PERMISSION_DENIED', 0);
define('PERMISSION_READ', 1);
define('PERMISSION_ADD', 2);
define('PERMISSION_UPDATE', 4);
define('PERMISSION_DELETE', 8);
$read_only = PERMISSION_READ;
$read_delete = PERMISSION_READ + PERMISSION_DELETE;
$full_rights = PERMISSION_DENIED + PERMISSION_READ + PERMISSION_ADD + PERMISSION_UPDATE + PERMISSION_DELETE;
$myrights = PERMISSION_READ;
$myrights += PERMISSION_UPDATE;
?>
it's the bitwise or operator, so you're operating with numbers in base 2 notation.
for example:
1000 | 0001 = 1001 ==> (8 | 1 = 9)
http://www.php.net/manual/en/language.operators.bitwise.php
In your code each permission is represented by one bit in a different position, I mean:
1 = 0001 = perm1
2 = 0010 = perm2
4 = 0100 = perm3
8 = 1000 = perm4
so doing or with those numbers gives you the number with all the permissions together. then if you wanna check if a permission is set you gotta do an and operation with the permission that you're checking, for example:
$user_perms = perm1 | perm3;
if ($user_perms & perm4) echo "user does not have this permission";
Okay, there is a difference. In order to see the difference you can just see how these numbers appear in binary:
0 = 00000000
1 = 00000001
2 = 00000010
4 = 00000100
8 = 00001000
As you can see, each of these numbers have only one bit set to one each in a different position.
Now, having a bitwise OR between them will make the result with one on each position as the operands:
00000010 |
00000100
----------
00000110
In this case it's the same as just adding the numbers.
0 | 0 = 0; 0 + 0 = 0
0 | 1 = 1 | 0 = 1 + 0 = 1
The difference comes here:
1 | 1 = 1
while
1 + 1 = 10 !!
The difference in this example is that bit-wise operator is faster because you just operate on the bits.
| is the bitwise OR (every bit of the result is 1 iff at least one of the corresponding bits in the inputs is 1). Using addition would work as well, but doesn't make it clear one is assembling a bitmask, and can lead to bugs, because
PERMISSION_READ + PERMISSION_READ != PERMISSION_READ
but
PERMISSION_READ | PERMISSION_READ == PERMISSION_READ
This is a bitwise OR operator.
You can find more information about it here : http://php.net/manual/en/language.operators.bitwise.php
Bitwise operators usual are good for soft encription methods

Categories