"<<" 是什么意思? PHP 中的意思?

发布于 2024-10-09 11:07:48 字数 319 浏览 0 评论 0 原文

可能的重复:
参考 - 这个符号在 PHP 中意味着什么?

是什么意思这行 PHP 中的 << 是什么意思?

$count = (1 << $count_log2) - 1;

Possible Duplicate:
Reference - What does this symbol mean in PHP?

What does the << mean in this line of PHP?

$count = (1 << $count_log2) - 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

野心澎湃 2024-10-16 11:07:48

这是左移运算符。

因此,在您的示例中,您将值 1 , $count_log2 次向左移动。
所以该值为 2^count_log2。

8位二进制中的1是00000001
因此,如果 $count_log2 = 4,我们需要得到 2^4 = 16。

左移意味着将 1 左移 4 次(因为 $count_log2 = 4)。
让我们执行步骤。

  1. 00000010(十进制2)
  2. 00000100(十进制4)
  3. 00001000(十进制8)
  4. 00010000(十进制16)

所以我们得到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.

  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.

满意归宿 2024-10-16 11:07:48

<<>> 是所谓的位移运算符。

<代码>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 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.

峩卟喜欢 2024-10-16 11:07:48

它是左移运算符。请参阅 PHP 手册的位运算符页面。

引用手册:

<代码>$a << $b - 左移 - 将 $a $b 的位向左移动一步(每一步意味着“乘以二”)

在这种特定情况下,$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:

$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

热情消退 2024-10-16 11:07:48

<<和>>称为位运算符,它们分别左移和右移一定的数字位。

在你的例子中:
1<<; $count_log2 会将数字 1 左移 $count_log2 的值。这在二进制中更容易看出,其中数字 1 表示为 8 位数字将是:

1 - 0000 0001

如果将此数字左移 3 (1 << 3),您将得到 8:

8 - 0000 1000

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
写给空气的情书 2024-10-16 11:07:48

向左移动或向右移动。请参阅有关按位运算符的手册。

Shift left or shift right. See the manual on bitwise operators.

是你 2024-10-16 11:07:48

它是一个用于向左移动位的按位运算符,这不仅仅是 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

这个俗人 2024-10-16 11:07:48

<< 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")

南薇 2024-10-16 11:07:48

'>>'和“<<”是按位运算符。
'>>'向右移动,并且 '<<'向左移动。

可以这样想,二进制 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文