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.
Related
How would one get the integer value of the n-th value from n?
This is hard to phrase so I'll just use English. If I wanted the 3rd integer value from 1...
1 = 1
2 = 3
3 = 4 <- (looking to get 4 using 3)
4 = 8
5 = 16 <- (or 16 using 5)
...
I could just do a lookup table, but I'm sure there's a better solution.
$bitvalue = 5;
$intvalue = 2 ** ($bitvalue - 1);
// gives 16
echo $intvalue;
The ** operator is the power operator. So I'm using powers of 2.
<?php
echo 2<<3; //Output 16
echo '---';
echo 3<<2; //Output 12
?>
Tried to find out logic. But its ends up in vain!! Can someone explain it please
The << operator is a bitwise operator. This basically means the numbers are treated as binary numbers, and the interaction is about moving bits around.
So let's have a look at the numbers and the operations:
First, 2 << 3
0b000010 // 2 in binary
0b010000 // move the bits three left, we get 16
Then 3 << 2
0b000011 // 3 in binary
0b001100 // move the bits two left, we get 12
From the manual page linked above:
Shift the bits of $a $b steps to the left (each step means "multiply by two")
So 3<<2 in effect means 3*(2^2), while 2<<3 means 2*(2^3).
The << operator is bitwise left shift.
Let's write the numbers in their binary representation
0000 0010 // 2
0000 0011 // 3
And then shift them by 3 and 2 respectively:
0001 0000 // 16
0000 1100 // 12
2 = 0b10
0b100 = 4
0b1000 = 8
0b10000 = 16
3 = 0b11
0b110 = 6
0b1100 = 12
The fist operator (<<) is the bitwise shift operator, specifically the left-shift operator. It takes the left hand argument and shifts the binary representation to the left by the number of bits specified by the right argument. Right-shift (>>) does the same but to the right. Read more about it here http://php.net/language.operators.bitwise
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
I wanted to concatenate 2 variables, and by error I typed another code and I got a strange result.
This is what looks like the code :
echo 'Hello World' | 'test';
Result : |e|o World
What the pipe sign do if not concatenated ?
According to the PHP manual
"|" is a "bitwise OR". Bitwise operators allow evaluation and manipulation of specific bits within an integer.
Example Name Result
$a | $b Or (inclusive or) Bits that are set in either $a or $b are set.
Example:
$a = 9;
$b = 10;
echo $a | $b;
This would output the number 11 as follows:
1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
$a 0 0 0 0 1 0 0 1 = 9
$b 0 0 0 0 1 0 1 0 = 10
$a | $b 0 0 0 0 1 0 1 1 = 11
If you notice we have 3 bits set, in the 8, 2, and 1 column.. add those up 8+2+1 and you get 11.
For mere string concatenation use the dot . operator.
Hope that clarifies it.
It's the OR bitwise operator
If you want to concat string you should use dot
echo "ABC" . "DEF";
Example of OR bitwise usage
// base 16 - result in 0x03
$result = 0x01 | 0x02;
// base 2
0000 0001
0000 0010
---------
0000 0011
That | means 'bitwise OR', which will convert the strings into binary, then overlay them on each other to calculate the result using logical OR for each position i.e. if either string has a 1 at that position, then the result will have a 1, otherwise, you'll get a 0.
In this case, it's doing this with the numerical ascii character codes of each character, which sometimes leads to new character codes and sometimes to garbage, which won't render. This is why the beginning of 'Hello world' is messed up, where it is overlaid with 'test', but the end is fine because it's not having any 1s added to it by another string at that point. See here for a more detailed example from the manual (uses XOR, but same idea).
Use . for concatenation.
I would guess that it's a bitwise OR
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.