希望这个小程序有助于使用它打印的示例更好地解释异或运算符:
>>> 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
>>>