[英]Understanding XOR logical operator
I don't understand this 我不明白
2.0.0p247 :616 > 5 ^ 2
=> 7
2.0.0p247 :617 > 5 ^ 1
=> 4
What 7 and 4 means in those scenarios? 在这些情况下,7和4是什么意思?
I try reading here http://en.wikipedia.org/wiki/Exclusive_disjunction but cannot figure out by looking into the diagrams what is the subtract here. 我尝试在此处阅读http://en.wikipedia.org/wiki/Exclusive_disjunction,但无法通过查看图表来找出此处的减法。 Sorry if this is simple math question. 抱歉,这是简单的数学问题。
It has to do with the binary representation of the values. 它与值的二进制表示形式有关。
5 = 0101
2 = 0010
1 = 0001
Now the XOR works like this: 现在,XOR的工作方式如下:
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
so to compute 5 ^ 2, let's apply the ^
operation to each column : 因此,为了计算5 ^ 2,我们将^
操作应用于每一列 :
0101 (this is 5)
0010 (this is 2)
----
0111 ==> which is the binary representation of 7
How did this work? 这项工作如何进行? In the leftmost column, we computed 0^0=0
. 在最左边的列中,我们计算了0^0=0
。 In the second column, 1^0=1
. 在第二列中, 1^0=1
。 In the third column 0^1=1
, and so on. 在第三列中, 0^1=1
,依此类推。
and 5 ^ 1 和5 ^ 1
0101 (this is 5)
0001 (this is 1)
----
0100 ==> which is the binary represenation of 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.