运算符“^”的用途是什么?
可能的重复:
^ 运算符的作用是什么?
>>> var foo = [1,2]
>>> var bar = [3,4]
>>> foo ^ bar
0
>>> foo ^ 3
3
>>> 1^3
2
运算符的用途是什么:^?
编辑1:你能解释一下为什么吗
>>> foo ^ bar
0
?
Possible Duplicate:
What does the ^ operator do?
>>> var foo = [1,2]
>>> var bar = [3,4]
>>> foo ^ bar
0
>>> foo ^ 3
3
>>> 1^3
2
What is the purpose of the operator: ^?
Edit 1: Can you explain why
>>> foo ^ bar
0
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
1^3
的情况下,XOR 运算符执行一些二进制操作以获得 2。JavaScript将数组语法
[x,y]
视为NaN 当你开始用它做数学的事情时。当您对其进行按位运算时,
NaN
被解释为0
,因此foo
和bar
数学开始进行考虑到这一点,这似乎是正确的。
[1,2]^7 = 7
、[1,2,3]^9 = 9
等。In the case of
1^3
, the XOR operator does some binary stuff to get 2.JavaScript sees the array syntax
[x,y]
asNaN
when you start doing math-y things with it.NaN
is interpreted as0
when you do bitwise operations on it, so thefoo
andbar
math starts to make sense taking that into account:Which seems to hold true.
[1,2]^7 = 7
,[1,2,3]^9 = 9
, etc.按位异或
Bitwise XOR
它称为按位运算符之一,它将其操作数视为 32 位(零和一)的序列,而不是十进制、十六进制或八进制数。按位 XOR (a ^ b) 在每个位位置返回一个 1,其中任一操作数(但不是两个操作数)的对应位都是 1。
编辑:
还有
Its called one of Bitwise operator,it treat their operands as a sequence of 32 bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers.Bitwise XOR (a ^ b) Returns a one in each bit position for which the corresponding bits of either but not both operands are ones.
EDIT:
and also