运算符“^”的用途是什么?

发布于 2024-12-11 06:43:01 字数 421 浏览 0 评论 0原文

可能的重复:
^ 运算符的作用是什么?

>>> 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 技术交流群。

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

发布评论

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

评论(3

锦欢 2024-12-18 06:43:01

1^3 的情况下,XOR 运算符执行一些二进制操作以获得 2。JavaScript

    1 = 00000001 ^
    3 = 00000011
        ========
        00000010 = 2

将数组语法 [x,y] 视为 NaN 当你开始用它做数学的事情时。当您对其进行按位运算时,NaN 被解释为 0,因此 foobar 数学开始进行考虑到这一点,

foo => NaN = 00000000 ^
bar => NaN = 00000000
             ========
             00000000 = 0

foo => NaN = 00000000 ^
         3 = 00000011
             ========
             00000011 = 3

这似乎是正确的。 [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.

    1 = 00000001 ^
    3 = 00000011
        ========
        00000010 = 2

JavaScript sees the array syntax [x,y] as NaN when you start doing math-y things with it. NaN is interpreted as 0 when you do bitwise operations on it, so the foo and bar math starts to make sense taking that into account:

foo => NaN = 00000000 ^
bar => NaN = 00000000
             ========
             00000000 = 0

foo => NaN = 00000000 ^
         3 = 00000011
             ========
             00000011 = 3

Which seems to hold true. [1,2]^7 = 7, [1,2,3]^9 = 9, etc.

听,心雨的声音 2024-12-18 06:43:01

它称为按位运算符之一,它将其操作数视为 32 位(零和一)的序列,而不是十进制、十六进制或八进制数。按位 XOR (a ^ b) 在每个位位置返回一个 1,其中任一操作数(但不是两个操作数)的对应位都是 1。

编辑:

a b a XOR b 
0 0 0 
0 1 1 
1 0 1 
1 1 0 

还有

 9 (base 10) = 00000000000000000000000000001001 (base 2)
 14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10)

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:

a b a XOR b 
0 0 0 
0 1 1 
1 0 1 
1 1 0 

and also

 9 (base 10) = 00000000000000000000000000001001 (base 2)
 14 (base 10) = 00000000000000000000000000001110 (base 2)
                   --------------------------------
14 ^ 9 (base 10) = 00000000000000000000000000000111 (base 2) = 7 (base 10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文