3

对于 CompSci 类,我们正在审查 Java Math 类。作为一个愚蠢的程序员,我尝试使用 ^ 运算符而不是 Math.pow 函数。惊喜,惊喜,它没有​​工作。但是,Java 吐出的是我的问题。我试图弄清楚正在用数字完成的操作。你可以在下面看到我在说什么。

System.out.println(5^1);
System.out.println(5^2);
System.out.println(5^3);
System.out.println(5^4);
System.out.println(5^5);
System.out.println(5^6);
System.out.println(5^7);
System.out.println(5^8);
System.out.println(5^9);

运行上述内容,我得到以下信息:

4
7
6
1
0
3
2
13
12

print 5^1当我在 Python (等)中做同样的事情时,也会发生同样的事情。Java API 文档说 ^ 是“按位异或”,但这仍然无助于从 5 和 3 获得 6。有人可以解释为什么会这样吗?

4

8 回答 8

15

这是按位运算,因此它对数字的二进制位进行运算。6 是110二进制形式。5 是101二进制形式。

110
101
=== (^ xor)
011

011在二进制中是 3。

阅读https://en.wikipedia.org/wiki/Exclusive_or

于 2012-10-25T17:27:45.597 回答
3

好的,让我们举第一个例子。最简单的一个。拿5^1

5可以像这样用二进制表示101,1可以像这样表示001

101    XOR
001
---
100 = 4 (in decimal)

因为和 。1 XOR 1 = 0_ 只需去查看 XOR 上的wiki页面。0 XOR 0 = 01 XOR 0 = 1

于 2012-10-25T17:30:57.370 回答
2

这是XOR 操作

5 = 101 in binary
3 = 011 in binary

    101
XOR 011 
  = 110 = 6 in decimal
于 2012-10-25T17:28:46.657 回答
1

在 Java 中,^应用于int参数的运算符是按位异或。这解释了输出。对于 5 和 3 的特殊情况:

101 (5)
011 (3)
---
110 (6)

请注意,对于逻辑参数,它是逻辑异或。在那种情况下(b1 ^ b2) == (b1 != b2)

于 2012-10-25T17:28:19.730 回答
1

^是 BitWise XOR 运算符。所以,

5 ^ 1: -

101 -> 5
001 -> 1
--- XOR
100 = 4
  • 1 XOR 1 = 0
  • 1 XOR 0 = 1

请参阅以下链接:-

于 2012-10-25T17:28:49.430 回答
1

^按位异或运算,它的真值表如下所示:

  | 0 | 1 |
0 | 0 | 1 |
1 | 1 | 0 |

所以输出位1只有当输入不同时,0否则。所以5 ^ 3看起来像这样:

  0000 0101 (5)
^ 0000 0011 (3)
= 0000 0110 (6)

所以它正在做它应该做的事情。请查看Nuts and Bolts 教程以了解按位运算此 SO 问题以获取更多参考。

于 2012-10-25T17:29:33.563 回答
1

这是因为二进制表示五个是101b,三个是011b。

Xor 的工作方式如下:

0 xor 0 = 0
0 xor 1 = 1
1 xor 0 = 1
1 xor 1 = 0

因此: 101 xor 011 = 110 是六的二进制表示。

于 2012-10-25T17:29:44.840 回答
1

希望这个小程序有助于使用它打印的示例更好地解释异或运算符:

>>> for x, y in ((x, y) for x in range(11) for y in range(x, 11)):
    print('''{0} ^ {1} = {2} (Why?)
Because:
{0:04b} = {0}
^^^^
{1:04b} = {1}
====
{2:04b} = {2}
'''.format(x, y, x ^ y))


0 ^ 0 = 0 (Why?)
Because:
0000 = 0
^^^^
0000 = 0
====
0000 = 0

0 ^ 1 = 1 (Why?)
Because:
0000 = 0
^^^^
0001 = 1
====
0001 = 1

0 ^ 2 = 2 (Why?)
Because:
0000 = 0
^^^^
0010 = 2
====
0010 = 2

0 ^ 3 = 3 (Why?)
Because:
0000 = 0
^^^^
0011 = 3
====
0011 = 3

0 ^ 4 = 4 (Why?)
Because:
0000 = 0
^^^^
0100 = 4
====
0100 = 4

0 ^ 5 = 5 (Why?)
Because:
0000 = 0
^^^^
0101 = 5
====
0101 = 5

0 ^ 6 = 6 (Why?)
Because:
0000 = 0
^^^^
0110 = 6
====
0110 = 6

0 ^ 7 = 7 (Why?)
Because:
0000 = 0
^^^^
0111 = 7
====
0111 = 7

0 ^ 8 = 8 (Why?)
Because:
0000 = 0
^^^^
1000 = 8
====
1000 = 8

0 ^ 9 = 9 (Why?)
Because:
0000 = 0
^^^^
1001 = 9
====
1001 = 9

0 ^ 10 = 10 (Why?)
Because:
0000 = 0
^^^^
1010 = 10
====
1010 = 10

1 ^ 1 = 0 (Why?)
Because:
0001 = 1
^^^^
0001 = 1
====
0000 = 0

1 ^ 2 = 3 (Why?)
Because:
0001 = 1
^^^^
0010 = 2
====
0011 = 3

1 ^ 3 = 2 (Why?)
Because:
0001 = 1
^^^^
0011 = 3
====
0010 = 2

1 ^ 4 = 5 (Why?)
Because:
0001 = 1
^^^^
0100 = 4
====
0101 = 5

1 ^ 5 = 4 (Why?)
Because:
0001 = 1
^^^^
0101 = 5
====
0100 = 4

1 ^ 6 = 7 (Why?)
Because:
0001 = 1
^^^^
0110 = 6
====
0111 = 7

1 ^ 7 = 6 (Why?)
Because:
0001 = 1
^^^^
0111 = 7
====
0110 = 6

1 ^ 8 = 9 (Why?)
Because:
0001 = 1
^^^^
1000 = 8
====
1001 = 9

1 ^ 9 = 8 (Why?)
Because:
0001 = 1
^^^^
1001 = 9
====
1000 = 8

1 ^ 10 = 11 (Why?)
Because:
0001 = 1
^^^^
1010 = 10
====
1011 = 11

2 ^ 2 = 0 (Why?)
Because:
0010 = 2
^^^^
0010 = 2
====
0000 = 0

2 ^ 3 = 1 (Why?)
Because:
0010 = 2
^^^^
0011 = 3
====
0001 = 1

2 ^ 4 = 6 (Why?)
Because:
0010 = 2
^^^^
0100 = 4
====
0110 = 6

2 ^ 5 = 7 (Why?)
Because:
0010 = 2
^^^^
0101 = 5
====
0111 = 7

2 ^ 6 = 4 (Why?)
Because:
0010 = 2
^^^^
0110 = 6
====
0100 = 4

2 ^ 7 = 5 (Why?)
Because:
0010 = 2
^^^^
0111 = 7
====
0101 = 5

2 ^ 8 = 10 (Why?)
Because:
0010 = 2
^^^^
1000 = 8
====
1010 = 10

2 ^ 9 = 11 (Why?)
Because:
0010 = 2
^^^^
1001 = 9
====
1011 = 11

2 ^ 10 = 8 (Why?)
Because:
0010 = 2
^^^^
1010 = 10
====
1000 = 8

3 ^ 3 = 0 (Why?)
Because:
0011 = 3
^^^^
0011 = 3
====
0000 = 0

3 ^ 4 = 7 (Why?)
Because:
0011 = 3
^^^^
0100 = 4
====
0111 = 7

3 ^ 5 = 6 (Why?)
Because:
0011 = 3
^^^^
0101 = 5
====
0110 = 6

3 ^ 6 = 5 (Why?)
Because:
0011 = 3
^^^^
0110 = 6
====
0101 = 5

3 ^ 7 = 4 (Why?)
Because:
0011 = 3
^^^^
0111 = 7
====
0100 = 4

3 ^ 8 = 11 (Why?)
Because:
0011 = 3
^^^^
1000 = 8
====
1011 = 11

3 ^ 9 = 10 (Why?)
Because:
0011 = 3
^^^^
1001 = 9
====
1010 = 10

3 ^ 10 = 9 (Why?)
Because:
0011 = 3
^^^^
1010 = 10
====
1001 = 9

4 ^ 4 = 0 (Why?)
Because:
0100 = 4
^^^^
0100 = 4
====
0000 = 0

4 ^ 5 = 1 (Why?)
Because:
0100 = 4
^^^^
0101 = 5
====
0001 = 1

4 ^ 6 = 2 (Why?)
Because:
0100 = 4
^^^^
0110 = 6
====
0010 = 2

4 ^ 7 = 3 (Why?)
Because:
0100 = 4
^^^^
0111 = 7
====
0011 = 3

4 ^ 8 = 12 (Why?)
Because:
0100 = 4
^^^^
1000 = 8
====
1100 = 12

4 ^ 9 = 13 (Why?)
Because:
0100 = 4
^^^^
1001 = 9
====
1101 = 13

4 ^ 10 = 14 (Why?)
Because:
0100 = 4
^^^^
1010 = 10
====
1110 = 14

5 ^ 5 = 0 (Why?)
Because:
0101 = 5
^^^^
0101 = 5
====
0000 = 0

5 ^ 6 = 3 (Why?)
Because:
0101 = 5
^^^^
0110 = 6
====
0011 = 3

5 ^ 7 = 2 (Why?)
Because:
0101 = 5
^^^^
0111 = 7
====
0010 = 2

5 ^ 8 = 13 (Why?)
Because:
0101 = 5
^^^^
1000 = 8
====
1101 = 13

5 ^ 9 = 12 (Why?)
Because:
0101 = 5
^^^^
1001 = 9
====
1100 = 12

5 ^ 10 = 15 (Why?)
Because:
0101 = 5
^^^^
1010 = 10
====
1111 = 15

6 ^ 6 = 0 (Why?)
Because:
0110 = 6
^^^^
0110 = 6
====
0000 = 0

6 ^ 7 = 1 (Why?)
Because:
0110 = 6
^^^^
0111 = 7
====
0001 = 1

6 ^ 8 = 14 (Why?)
Because:
0110 = 6
^^^^
1000 = 8
====
1110 = 14

6 ^ 9 = 15 (Why?)
Because:
0110 = 6
^^^^
1001 = 9
====
1111 = 15

6 ^ 10 = 12 (Why?)
Because:
0110 = 6
^^^^
1010 = 10
====
1100 = 12

7 ^ 7 = 0 (Why?)
Because:
0111 = 7
^^^^
0111 = 7
====
0000 = 0

7 ^ 8 = 15 (Why?)
Because:
0111 = 7
^^^^
1000 = 8
====
1111 = 15

7 ^ 9 = 14 (Why?)
Because:
0111 = 7
^^^^
1001 = 9
====
1110 = 14

7 ^ 10 = 13 (Why?)
Because:
0111 = 7
^^^^
1010 = 10
====
1101 = 13

8 ^ 8 = 0 (Why?)
Because:
1000 = 8
^^^^
1000 = 8
====
0000 = 0

8 ^ 9 = 1 (Why?)
Because:
1000 = 8
^^^^
1001 = 9
====
0001 = 1

8 ^ 10 = 2 (Why?)
Because:
1000 = 8
^^^^
1010 = 10
====
0010 = 2

9 ^ 9 = 0 (Why?)
Because:
1001 = 9
^^^^
1001 = 9
====
0000 = 0

9 ^ 10 = 3 (Why?)
Because:
1001 = 9
^^^^
1010 = 10
====
0011 = 3

10 ^ 10 = 0 (Why?)
Because:
1010 = 10
^^^^
1010 = 10
====
0000 = 0

>>> 
于 2012-10-25T18:52:01.930 回答