What do these JavaScript bitwise operators do?

后端 未结 3 2082
余生分开走
余生分开走 2020-11-22 10:36
  • x <<= y (x = x << y)
  • x >>= y (x = x >> y)
  • x >>>= y (x = x >>> y)
3条回答
  •  佛祖请我去吃肉
    2020-11-22 11:29

    <<, >>
    

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

提交回复
热议问题