运算符“>>”(双箭头)和“|”有什么作用 (单管)在 JavaScript 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10422744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:49:47  来源:igfitidea点击:

What do the operators ">>" (double arrow) and "|" (single pipe) mean in JavaScript?

javascript

提问by benhowdle89

I saw this in some JS code:

我在一些 JS 代码中看到了这一点:

        index = [
            ascii[0] >> 2,
            ((ascii[0] & 3) << 4) | ascii[1] >> 4,
            ((ascii[1] & 15) << 2) | ascii[2] >> 6,
            ascii[2] & 63
        ];

I'd quite like to know what a lot of this means. Specifically ">>", a single pipe "|" and the "&" symbol on the last line?

我很想知道这意味着什么。特别是“>>”,单个管道“|” 最后一行的“&”符号?

Much appreciated!

非常感激!

回答by Niet the Dark Absol

x >> ymeans to shift the bits of xby yplaces to the right (<<to the left).

x >> y手段的位移位x通过y的地方(右侧<<到左侧)。

x | ymeans to compare the bits of xand y, putting a 1in each bit if either xor yhas a 1in that position.

x | y表示比较x和 的位,如果其中一个或在该位置有一个,则在每个位中y放置一个。1xy1

x & yis the same as |, except that the result is 1if BOTH xand yhave a 1.

x & y与 相同|,除了结果是1如果 BOTHx并且y有一个1.

Examples:

例子:

#left-shifting 1 by 4 bits yields 16
1 << 4 = b00001 << 4 = b10000 = 16

#right-shifting 72 by 3 bits yields 9
72 >> 3 = b1001000 >> 3 = b1001 = 9

#OR-ing 
8 | 2 = b1000 | b0010 = b1010 = 10

#AND-ing
6 & 3 = b110 & b011 = b010 = 2

For more information, search Google for "bitwise operators".

有关更多信息,请在Google 中搜索“按位运算符”

回答by Ry-

>>is a right bitwise shift. It takes the bits and shifts them right nplaces1. For example, let's examine 35 >> 2:

>>是按位右移。它需要这些位并将它们右移n 个位置1。例如,让我们检查35 >> 2

35 = 100011 shift two places
     001000 = 8

And indeed, 35 >> 2 == 8.

确实,35 >> 2 == 8



|is a bitwise OR. It takes each bit in each operand and ORs them together. You can envision it as a sort of binary addition, but you don't carry when both top and bottom are 1. For example, here's 5 | 3:

|是按位或。它获取每个操作数中的每一位并将它们组合在一起。你可以把它想象成一种二进制加法,但是当 top 和 bottom 都是1. 例如,这里是5 | 3

5 = 101
3 = 011
| -----
    111 = 7

And indeed, 5 | 3 == 7.

确实,5 | 3 == 7



Finally, &is a bitwise AND. It takes each bit in each operand, except instead of giving 1 if either one bit OR the other is one, it gives 1 if one bit AND the other are both one. For example, here's 5 & 3:

最后,&是按位与。它取每个操作数中的每一位,除非如果一位或另一个是 1,则不是给 1,而是如果一位和另一个都是 1,则给 1。例如,这里是5 & 3

5 = 101
3 = 011
& -----
    001 = 1

Try it out; 5 & 3 == 1.

试试看; 5 & 3 == 1.



Some other ones you might want to be aware of are <<, which is a left bitwise shift, and ^, which is an XOR (0 when both bits are the same, 1 if they're different).

您可能想知道的其他一些是<<,这是一个左移,以及^,这是一个异或(当两个位相同时为 0,如果它们不同则为 1)。

1Actually, it's n modulo 32. 1 >> 32is 1. Not sure why.

1实际上,它是 n 模 32。1 >> 321。不知道为什么。

回答by Kendall Frey

The >>and <<operators are a bitwise shift. For example,

>><<运营商是一个按位偏移。例如,

11 =      00001011
11 << 3 = 01011000 = 88

It is worth noting that m << n = m * 2^nand m >> n = m / 2^n. This is sometimes used to do very efficient multiplication/division by powers of 2.

值得注意的是m << n = m * 2^nm >> n = m / 2^n。这有时用于进行非常有效的乘法/除以 2 的幂。

The &and |are bitwise and and or respectively.

&|是按位和和或分别。

11 =      00001011
28 =      00011100
11 & 28 = 00001000 = 8

11 =      00001011
28 =      00011100
11 | 28 = 00011111 = 31

While I'm at it, I should mention the ^operator, which is not used for power, but for bitwise exclusive-or.

在此期间,我应该提到^运算符,它不用于幂,而是用于按位异或。

11 =      00001011
28 =      00011100
11 ^ 28 = 00010111 = 23

回答by German Latorre

  • & (Bitwise AND)
  • | (Bitwise OR)
  • << (Left shift)
  • >> (Sign-propagating right shift)
  • &(按位与)
  • | (按位或)
  • <<(左移)
  • >>(符号传播右移)

Examples (from https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators):

示例(来自https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators):

Bitwise and:

按位和:

     9 (base 10) = 00000000000000000000000000001001 (base 2)
    14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 & 9 (base 10) = 00000000000000000000000000001000 (base 2) = 8 (base 10)

Left shift (9 << 2 shifts bits of 9 in binary, 2 bits to the left):

左移(9 << 2 以二进制方式将 9 的位向左移动 2 位):

     9 (base 10): 00000000000000000000000000001001 (base 2)
                  --------------------------------
9 << 2 (base 10): 00000000000000000000000000100100 (base 2) = 36 (base 10)

回答by Kris Krause

Looks like bitwise operators to me:

对我来说看起来像位运算符:

http://web.eecs.umich.edu/~bartlett/jsops.html

http://web.eecs.umich.edu/~bartlett/jsops.html

Edit: that ascii array was a dead give away... LOL

编辑:那个 ascii 数组是一个死的放弃......大声笑