这些 JavaScript 位运算符有什么作用?

发布于 2024-10-09 09:37:41 字数 343 浏览 0 评论 0原文

  • x <<= y (x = x << y)
  • x >>= y (x = x >> y)
  • x >>>= y (x = x >>> y)
  • x &= y (x = x & y)
  • x ^= y (x = x ^ y)
  • x |= y (x = x | y)

这些不同的运算符有什么作用?

  • x <<= y (x = x << y)
  • x >>= y (x = x >> y)
  • x >>>= y (x = x >>> y)
  • x &= y (x = x & y)
  • x ^= y (x = x ^ y)
  • x |= y (x = x | y)

What do these different operators do?

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

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

发布评论

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

评论(2

童话 2024-10-16 09:37:41
<<, >>

分别向左和向右位移。如果您将左操作数想象为二进制位序列,则将其向左或向右移动右操作数指示的位数。

&, ^, |

它们分别是按位异或。您可以将 &| 视为 &&|| 的对应项,只不过他们将其操作数视为位向量,并对每个位执行逻辑运算。没有 ^^ 运算符,但此操作是“xor”或“异或” 。您可以将“a xor b”视为“a 或 b,但不能同时两者”。

<<, >>

Bit shift left and right, respectively. If you imagine the left operand as a binary sequence of bits, you are shifting those to the left or right by the number of bits indicated by the right operand.

&, ^, |

These are bitwise and, xor, and or, respectively. You can think of & and | as the counterparts to && and ||, except that they will treat their operands as bit vectors, and perform the logical operations on each of the bits. There is no ^^ operator, but this operation is "xor" or "exclusive or". You can think of "a xor b" as "a or b, but not both".

感性 2024-10-16 09:37:41

这是让初学者变得简单的尝试。

先决条件

您必须熟悉二进制数字系统(由两位数字组成的数字)。如果您不是,请先检查此链接: https://www.mathsisfun.com/二进制数系统.html。以防万一之前的链接中断,这个答案可能会有所帮助:https://stackoverflow.com/a/32155850/1636522。

事实上,为了弄清楚这些运算符是如何工作的,您需要知道运算中涉及的数字后面是哪个位序列。之后你应该能够理解以下内容。

提醒

十进制数字及其二进制表示法:

0    0 | 5  101
1    1 | 6  110
2   10 | 7  111
3   11 | 8 1000
4  100 | 9 1001

>>>>>< 有何作用;< do?

这些运算符将位序列向左或向右移位。

 decimal | binary      decimal | binary 
---------|---------   ---------|---------
       9 |    1001           2 |      10
    >> 2 | >>    2        << 2 | <<    2
     = 2 |  =   10         = 8 |  = 1000

&|^ 的作用是什么?

这些运算符组合两个数字的位以创建一个新的数字数字。

 decimal | binary     decimal | binary     decimal | binary
---------|--------   ---------|--------   ---------|--------
       5 |    101           5 |    101           5 |    101
     & 6 |  & 110         | 6 |  | 110         ^ 6 |  ^ 110
     = 4 |  = 100         = 7 |  = 111         = 3 |  = 011

& 如何工作?

对于每对位:如果两个位中至少有一个为 0,则结​​果位为 0。如果两个位均不为 0,则结​​果位为 0。 0,结果位为 1。

  101    bit 3 | bit 2 | bit 1
& 110   -------|-------|-------
= 100      1   |   0   |   1
           &   |   &   |   &
           1   |   1   |   0
           =   |   =   |   =
           1   |   0   |   0

| 如何工作?

对于每对位:如果两个位中至少有一个为 1,则结果位为 1。如果两个位都不为 1,则结果位为 0。

  101    bit 3 | bit 2 | bit 1
| 110   -------|-------|-------
= 111      1   |   0   |   1
           |   |   |   |   |
           1   |   1   |   0
           =   |   =   |   =
           1   |   1   |   1

^ 如何工作?

对于每对位:如果两个位不同,则结果位为 1。如果两个位相同,则结果位为 0。

  101    bit 3 | bit 2 | bit 1
^ 110   -------|-------|-------
= 011      1   |   0   |   1
           ^   |   ^   |   ^
           1   |   1   |   0
           =   |   =   |   =
           0   |   1   |   1

Here is an attempt to make things simple for the very beginner.

Prerequisites

You have to be familiar with the binary number system (numbers made of two digits). If you are not then check this link first: https://www.mathsisfun.com/binary-number-system.html. Just in case the previous link breaks, this answer may help a little: https://stackoverflow.com/a/32155850/1636522.

Indeed, in order to figure out how these operators work, you need to know which bit sequence is behind the numbers involved in the operation. After that you should be able to understand the following stuffs.

Reminder

Decimal digits and their binary notations:

0    0 | 5  101
1    1 | 6  110
2   10 | 7  111
3   11 | 8 1000
4  100 | 9 1001

What do >>>, >> and << do?

These operators shift a bit sequence to the left or to the right.

 decimal | binary      decimal | binary 
---------|---------   ---------|---------
       9 |    1001           2 |      10
    >> 2 | >>    2        << 2 | <<    2
     = 2 |  =   10         = 8 |  = 1000

What do &, | and ^ do?

These operators combine the bits of two numbers to create a new number.

 decimal | binary     decimal | binary     decimal | binary
---------|--------   ---------|--------   ---------|--------
       5 |    101           5 |    101           5 |    101
     & 6 |  & 110         | 6 |  | 110         ^ 6 |  ^ 110
     = 4 |  = 100         = 7 |  = 111         = 3 |  = 011

How does & work?

For each pair of bits: If at least one of the two bits is 0, the resulting bit is 0. If none of the two bits is 0, the resulting bit is 1.

  101    bit 3 | bit 2 | bit 1
& 110   -------|-------|-------
= 100      1   |   0   |   1
           &   |   &   |   &
           1   |   1   |   0
           =   |   =   |   =
           1   |   0   |   0

How does | work?

For each pair of bits: If at least one of the two bits is 1, the resulting bit is 1. If none of the two bits is 1, the resulting bit is 0.

  101    bit 3 | bit 2 | bit 1
| 110   -------|-------|-------
= 111      1   |   0   |   1
           |   |   |   |   |
           1   |   1   |   0
           =   |   =   |   =
           1   |   1   |   1

How does ^ work?

For each pair of bits: If the two bits are different, the resulting bit is 1. If the two bits are the same, the resulting bit is 0.

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