这些 JavaScript 位运算符有什么作用?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
分别向左和向右位移。如果您将左操作数想象为二进制位序列,则将其向左或向右移动右操作数指示的位数。
它们分别是按位和、异或和或。您可以将
&
和|
视为&&
和||
的对应项,只不过他们将其操作数视为位向量,并对每个位执行逻辑运算。没有^^
运算符,但此操作是“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".这是让初学者变得简单的尝试。
先决条件
您必须熟悉二进制数字系统(由两位数字组成的数字)。如果您不是,请先检查此链接: https://www.mathsisfun.com/二进制数系统.html。以防万一之前的链接中断,这个答案可能会有所帮助:https://stackoverflow.com/a/32155850/1636522。
事实上,为了弄清楚这些运算符是如何工作的,您需要知道运算中涉及的数字后面是哪个位序列。之后你应该能够理解以下内容。
提醒
十进制数字及其二进制表示法:
>>>
、>>
和< 有何作用;<
do?这些运算符将位序列向左或向右移位。
&
、|
和^
的作用是什么?这些运算符组合两个数字的位以创建一个新的数字数字。
&
如何工作?对于每对位:如果两个位中至少有一个为 0,则结果位为 0。如果两个位均不为 0,则结果位为 0。 0,结果位为 1。
|
如何工作?对于每对位:如果两个位中至少有一个为 1,则结果位为 1。如果两个位都不为 1,则结果位为 0。
^
如何工作?对于每对位:如果两个位不同,则结果位为 1。如果两个位相同,则结果位为 0。
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:
What do
>>>
,>>
and<<
do?These operators shift a bit sequence to the left or to the right.
What do
&
,|
and^
do?These operators combine the bits of two numbers to create a new number.
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.
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.
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.