What is $a & $b and $a | $b in php? - php

What happend $a & $b and $a | $b in php ?
$a = 11;
$b = 7;
echo $a & $b;
In the above php code it will give result 3 How it will calculate 3 ?
$a = 11;
$b = 7;
echo $a | $b;
In the above php code it will give result 15 How it will calculate 15?
Explain me both condition in php?

What's happening here is something called Bitwise Operators. It's more often used in much lower level languages, and it's one of the very simplest things a computer can do. Most computation is built on top of it. So what is it? Well, & is the Bitwise AND operator, and | is the Bitwise OR operator. You can test them with this online tool. But let's breakdown how it works.
AND Operator
Take two binary strings and any 1's that aren't in both binaries, the the same place, become a 0.
7 is 111
11 is 1011
So if you perform an AND on them, you get something like this
0111 &
1011 =
0011
0011 in Decimal is 3. You get 0011 because only the last two places are BOTH 1.
OR Operator
OR is basically the opsite. If a position in either binary is 1, then the output is 1. So when you perform it on 7 and 11, you'll get
1011 |
0111 =
1111
And 1111 is 15 in decimal

In binary, 11 is 00001011 and 7 is 00000111 (showing only relevant 8 bits for simplicity).
So 11 & 7 (bitwise AND)
00001011
00000111 & matching only where both matching bits are `1`
--------
00000011
which is the binary for 3
So 11 | 7 (bitwise OR)
00001011
00000111 | matching where either (or both) bits is `1`
--------
00001111
which is the binary for 15

Related

PHP if statement question (& operator)

I'm using a function I found online. What does the & mean in this conditional?
if ($strength & 8) {
$consonants .= '##$%';
}
$strength is supposed to be a number 0-8. The function is intending to use all $consonants concatenations where $strength < 8. (might explain why the function is not working).
A single & is the bitwise operator and the double && is the logical. (i.e. Bits that are set in both $strength and 8 are set in your example.) It's a lot more complicated than just saying that and it requires an understanding of how binary works.
EDIT: Check out this article for more information on Bitwise operators.
& is a bitwise operator - it's checking to see if the bits that total 8 are set. In this case, 1000
& is a bitwise operator. It combines two values bitwise.
What is a bitwise operator?
Every integer is internally represented as a number of bits.
1 is 0001
2 is 0010
4 is 0100
8 is 1000
And so on. every bit's value is twice as big as the one preceding it.
You can get other numbers by combining bits
3 is 0011 (2+1)
5 is 0101 (4+1)
A bitwise operation works on every bit in both variables. & sets every bit in the result to 1 if it is 1 in both values it operates on.
9&5 == 1
because
9 == 1001
5 == 0101
----------
1 == 0001
| will COMBINE all 1s:
3|5 == 7
3 == 0011
5 == 0101
---------
7 == 0111
How can yo use it?
Example:
define('LOG_WARNING',1);
define('LOG_IO',2);
define('LOG_ALIENATTACKS,4);
$myLogLevel = LOG_WARNING | LOG_ALIENATACKS;
Now $myLogLevel is a combination of LOG_WARNING and LOG_ALIENATTACK. You can test it with the & operator:
if($myLogLevel&LOG_WARNING).... //true
if($myLogLevel&LOG_IO).... //false
if($myLogLevel&LOG_ALIENATTACKS)..../ /true run or your live!!!
If you want to know more about the topic search for bitflags and binary operations.

PHP Operator <<

What does the << Operator mean in php?
Example:
$t = 5;
$foo = 1 << ($t);
echo($foo);
echo produces: 32
It 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-hand argument, for example:
1 << 2 = 4
because 1 (decimal) is 1 (binary); left-shift twice makes it 100 which is 4 in decimal.
1 << 5 = 32
because 100000 in binary is 32 in decimal.
Right shift (>>) does the same thing but to the right.
Easy trick to get result of the left shift operation, e.g.
15 << 2 = 15 * (2*2) = 60
15 << 3 = 15 * (2*2*2) = 120
15 << 5 = 15 * (2*2*2*2*2) = 480
and so on..
So it's:
(number on left) multiplied by (number on right) times 2.
Same goes for right shift operator (>>), where:
(number on left) divided by (number on right) times 2
"<<" is a bit-shift left. Please review PHP's bitwise operators. http://php.net/manual/en/language.operators.bitwise.php
A more in-depth explanation:
This means multiply by two because it works on the binary level. For instance, if you have the number 5 in binary
0101
and you bit-shift left once to (move each bit over one position)
1010
then your result is 10. Working with binary (from right to left) is 2^0, 2^1, 2^2, 2^3, and so on. You add the corresponding power of two if you see a 1. So our math for our new result looks like this:
0 + 2^1 + 0 + 2^3
0 + 2 + 0 + 8 = 10
It is the binary shifting operator:
http://php.net/manual/en/language.operators.bitwise.php
<< Bitwise left shift. This operation shifts the left-hand operand’s bits
to the left by a number of positions equal to the right operand,
inserting unset bits in the shifted positions.
>> Bitwise right shift. This operation shifts the left-hand operand’s bits
to the right by a number of positions equal to the right operand,
inserting unset bits in the shifted positions.
NOTE: It’s also interesting to note that these two provide an easy (and very fast)
way of multiply/divide integers by a power of two. For example: 1<<5 will give 32 as a result.......

What does >> mean in PHP?

Consider:
echo 50 >> 4;
Output:
3
Why does it output 3?
50 in binary is 11 0010, shift right by 4 yields 11 which is equal to 3.
See PHP documentation and Wikipedia.
As documented on php.org, the >> operator is a bitwise shift operator which shifts bits to the right:
$a >> $b - Shift the bits of $a $b steps to the right (each step means "divide by two")
50 in binary is 110010, and the >> operator shifts those bits over 4 places in your example code. Although this happens in a single operation, you could think of it in multiple steps like this:
Step 1 - 00011001
Step 2 - 00001100
Step 3 - 00000110
Step 4 - 00000011
Since binary 11 is equal to 3 in decimal, the code outputs 3.
Arithmetic shift right.
The >> operator is called a binary right shift operator.
Shifting bits to the right 4 times is the same as dividing by two, four times in a row. The result, in this case would be 3.125. Since 50 is an int, bit shifting will return the floor of this, which is 3.
Put another way, 50 is 0b110010 in binary. Shifted 4 times we have 0b11, which is 3 in decimal.
>> is the binary right-shift operator.
Your statement shifts the bits in the numeric value 50 four places to the right. Because all integers are represented in two's complement, this equals 3. An easy way to remember this is that one shift to the right is the same as dividing by 2, and one shift to the left is the same as multiplying by 2.
It's called a right shift.
'The bits of the left operand are shifted right by the number of positions of the right operand. The bit positions vacated on the left are filled with the sign bit, and bits shifted out on the right are discarded.'
Information can be found on it here:
http://php.comsci.us/etymology/operator/rightshift.php
It shifts the bits down four places.
50 in binary is 110010.
Shifted down four places is 11, which is 3.
For your convenience, one of the fastest ways to calculate the outputted value from a bitwise shift is to multiply or divide by 2.
For example echo 50 >> 4;
Given that this is a bitwise right, it literally means that the value will be decrease, then we can get the output by divide 50 for 2 and 4 times.
echo 50 >> 4; // 50/(2*2*2*2) ~ 3.
Given that (from) 48 -> (to) 63/16(2*2*2*2), the result will be more than 2 and less than 4. Then
echo 48 >> 4; // 48/(2*2*2*2) ~ 3.
echo 63 >> 4; // 63/(2*2*2*2) ~ 3.
However, when bitwise left, the result will be totally different as it multiplies by 2 with n times:
If echo 50 << 4; // 50*(2*2*2*2) ~ 800
If echo 51 << 4; // 51*(2*2*2*2) ~ 816
Live example: https://3v4l.org/1hbJe

How can I write 'n <<= 1' (Python) in PHP?

I have the Python expression n <<= 1
How do you express this in PHP?
That statement is short for
n = n << 1;
the << operator is means a bitwise shift left, by n positions. Its counterpart is >>, which means shift right by n. To visualize, say you have the value 5, and you want to shift it left by 2 positions. In binary:
0000 0101 -> 5
shift left by 2:
0001 0100 -> 20
Basically, you shift all bits in the given direction, and pad with zeroes. More or less equivalent, if you don't have a bitwise shift operator (which is common in most, if not all languages), is multiplying by 2^n for shift left, and dividing by 2^n for shift right.
In the example, you can see that: 5 * 2^2 = 5 * 4 = 20.
It's the same operator in php. $n <<= 1;
$n <<= 1; is valid php

What does the bitwise code "$n & ($n - 1)" do?

What does this code mean and what are other ways accomplish the same without using bit shifting?
if ($n & ($n - 1))
That formula checks to see whether a number is a power of 2 (if your condition as written is true, then the number is not a power of two).
Stated another way, your test checks to see whether there is more than one "1" bit set in the binary representation of $n. If there is zero or only one bit set, then your test will be false.
It is by far the most efficient way to determine that property.
First, this code is valid PHP, so your title is poor.
Second, the binary arithmetic going on looks something like this:
42 = 101010
&
41 = 101001
-----------
40 = 101000
Like Greg states this is the fastest way to check for a power of 2 number, but the code you've given checks to see if the number is not a power of 2. This can easily be ascertained by PHP's policy of: any non-null/non-zero value is true.
When we use ($n & ($n - 1)) then it converts $n & ($n-1) to its binary values and does binary AND operation.
Example
3 = 0011
4 = 0100
5 = 0101
3 = 0011
&
4 = 0100
------------
0
4 = 0100
&
5 = 0101
-----------
100
To check if given number is power of 2 or not we alway use formulae
($n & ($n - 1) == 0) which means ANDing of $n & $n-1 is equals to 0 or not.
Online AND Operation Calculator

Categories