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.
Related
I am trying to figure out why if I shift the negative integer -1 I always get -1, e.g.:
echo -1 >> 64; // -1
echo -1 >> 5; // -1
echo -1 >> 43; // -1
echo -1 >> 1; // -1
Whatever second operand of the right shift is given, -1 remains -1... I do understand that when you perform a right shift you're actually doing this:
x >> y = x / 2^y
But in the case of x being -1, if so, I do:
-1 >> 3 = -1 / 2^3
Shouldn't this value be -1/8 = -0.125?
Thanks for the attention.
Bitwise shift operators don't divide. They do what they are supposed to do - shift bits. In particular, the right shift operator does the following:
for each bit starting from the right, set its value to what's on the left of it
for the leftmost bit, which has nothing on the left, keep its current value
For example, if your number is
1011...101
right shift gives you
11011...10
So the rightmost bit (LSB) is lost and the leftmost bit (MSB) is duplicated. This is called "sign propagation", because MSB is used to distinguish positive (MSB=0) from negative (MSB=1) numbers.
Negative numbers are stored as "two's complement", that is, on a 32-bit system, -x is stored as 2^32-x. So, -1 is 10...00 (32 zeroes) - 1 == 1...1 (32 ones). If you shift 32 ones according to the above procedure, you get 32 ones yet again, that is, -1 >> whatever will always be -1.
The difference between the right shift and division by two is that the shift gives same results for odd and even numbers. Since the rightmost bit is lost, when you shift an odd number (which has LSB=1), the result is the same as shifting the next lower even number (the same combination of bits, but with LSB=0). So, you get no halves when you shift, since the dividend is forced to be even. For example,
1010 = 10102, 10 / 2 = 5.0 and 10 >> 1 == 510 == 1012
1110 = 10112, 11 / 2 = 5.5 but 11 >> 1 == 510 == 1012
If you prefer to think about x >> 1 in terms of division, it first "rounds" x down to an even number (x - abs(x) % 2) and then divides that number by two.
For x = -1 this gives you (-1 - abs(-1) % 2)/2 == (-1 - 1)/2 = -2/2 = -1.
It is the same in all languages I know - bitwise arithmetic right shift for -1 will be -1, and as others mentioned, this operation can be applied only to integers.
-1 is represented in binary as all bits filled with value 1. For arithmetic right shift the bits will be shifted to the right, and the highest bit (at the left) will be filled with sign of the value, for negative values it will be 1, and for positive - 0. So it makes -1 again after the shift.
There are other kinds of bitwise shifts, and for logical right shift the highest bit would be filled with zero. You can get some more info here: http://en.wikipedia.org/wiki/Bitwise_operation#Arithmetic_shift
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.......
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
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
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