开发者

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

开发者 https://www.devze.com 2023-02-02 00:55 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Reference - What does this symbol mean in PHP?
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 << $coun开发者_JAVA百科t_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.

  1. 00000010 (2 in decimal)
  2. 00000100 (4 in decimal)
  3. 00001000 (8 in decimal)
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消