Explain simple php code - php

I'm trying to figure out what this does, and why the <<
$mem_level_id = 1 << intval($iMembId);

<< is the bitwise left shift operator.
The number 1 in binary is 0000 0001. If intval($iMembId) is 5, the binary value for 1 would get left-shifted 5 places and end up like 0010 0000, which is the number 32.

The "<<" is a bit-shift left. Please review http://php.net/manual/en/language.operators.bitwise.php

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.

What does "<<" mean in PHP? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
What does the << mean in this line of PHP?
$count = (1 << $count_log2) - 1;
this is the shift left operator.
so in your example you are shifting left the value 1 , $count_log2 times to the left.
so the value is 2^count_log2.
1 in 8 bit binary is 00000001
so if $count_log2 = 4, we need to get 2^4 = 16.
shifting left means moving the 1 left 4 times (since $count_log2 = 4).
lets do the steps.
00000010 (2 in decimal)
00000100 (4 in decimal)
00001000 (8 in decimal)
00010000 (16 in decimal)
so we got 2^4.
a common reason for using shift operation is that it takes less time for the processor to do a shift operation than using multiplication.
Left Bitshift, http://php.net/manual/en/language.operators.bitwise.php
<< and >> are the so-called bitshift operator.
x << n shifts the bits in the integer x n places to the left, effectively multiplying x with 2 to the power of n.
Similarly x >> n shifts to the left, dividing x by 2 to the power of n.
It is a left bitshift operator. See the Bitwise Operators page of the PHP manual.
To quote the manual:
$a << $b - Shift left - Shift the bits of $a $b steps to the left (each step means "multiply by two")
In this specific case, $count = (1 << $count_log2) - 1 is the same as setting $count to pow(2, $count_log2) - 1
The << and >> are called Bitwise operators, they shift left and right respectively by a certain number of bits.
In your example:
1 << $count_log2 will shift the number 1 left by the value of $count_log2. This is easier to see in binary where the number 1 represented as an 8-bit number would be:
1 - 0000 0001
If you shift this number left by 3 (1 << 3) you would get 8:
8 - 0000 1000
Shift left or shift right. See the manual on bitwise operators.
Its a bitwise operator for shifting bits to the left, this is not just php but many languages use this for if binary manipulation
http://php.net/manual/en/language.operators.bitwise.php
<< is shift left operator in PHP
$a << $b means shift the bits of $a $b steps to the left (each step means "multiply by two")
'>>' and '<<' are bitwise operators.
'>>' shifts to the right, and '<<' shifts to the left.
Think of it like this, in binary 25 is 00011001.
if you performed a shift left on 25, you'd have 00110010, which is 50.
If you performed a shift right on 50, you'd have 25.

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

Categories