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:
发布评论
评论(9)
这是左移运算符。
因此,在您的示例中,您将值 1 , $count_log2 次向左移动。
所以该值为 2^count_log2。
8位二进制中的1是00000001
因此,如果 $count_log2 = 4,我们需要得到 2^4 = 16。
左移意味着将 1 左移 4 次(因为 $count_log2 = 4)。
让我们执行步骤。
所以我们得到2^4。
使用移位运算的一个常见原因是处理器执行移位运算比使用乘法花费的时间更少。
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.
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.
左移位,http://php.net/manual/en/language.operators。按位.php
Left Bitshift, http://php.net/manual/en/language.operators.bitwise.php
<<
和>>
是所谓的位移运算符。<代码>x << n 将整数
x
n
位中的位向左移动,有效地将x
乘以 2 的次方>n
。类似地
x>> n
向左移动,将x
除以 2 的n
次方。<<
and>>
are the so-called bitshift operator.x << n
shifts the bits in the integerx
n
places to the left, effectively multiplyingx
with 2 to the power ofn
.Similarly
x >> n
shifts to the left, dividingx
by 2 to the power ofn
.它是左移运算符。请参阅 PHP 手册的位运算符页面。
引用手册:
在这种特定情况下,
$count = (1 << $ count_log2) - 1
与将$count
设置为pow(2, $count_log2) - 1
相同It is a left bitshift operator. See the Bitwise Operators page of the PHP manual.
To quote the manual:
In this specific case,
$count = (1 << $count_log2) - 1
is the same as setting$count
topow(2, $count_log2) - 1
<<和>>称为位运算符,它们分别左移和右移一定的数字位。
在你的例子中:
1<<; $count_log2 会将数字 1 左移 $count_log2 的值。这在二进制中更容易看出,其中数字 1 表示为 8 位数字将是:
如果将此数字左移 3 (1 << 3),您将得到 8:
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:
If you shift this number left by 3 (1 << 3) you would get 8:
向左移动或向右移动。请参阅有关按位运算符的手册。
Shift left or shift right. See the manual on bitwise operators.
它是一个用于向左移动位的按位运算符,这不仅仅是 php,许多语言都使用它来进行二进制操作
http://php.net/manual/en/language.operators.bitwise.php
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
<< PHP 中的左移运算符
$a << $b 表示将 $a $b 的位向左移动一步(每一步表示“乘以二”)
<< 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")
'>>'和“<<”是按位运算符。
'>>'向右移动,并且 '<<'向左移动。
可以这样想,二进制 25 是 00011001。
如果您在 25 上执行左移,您将得到 00110010,即 50。
如果您在 50 上执行右移,您将得到 25。
'>>' 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.