交换a,b两值的方法:
a = a^b;
b = a^b;
a = a^b;
b = a ^ b = (a^b)^b=a^b^b=a^(b^b)=a^0=a,这样就成功的实现了b = a;接着又用a = a^b = (a^b)^b(第一个b还是原来的b,而第二个b已经是a的值,因为前面已经实现了交换)= (a^b)^a = a^a^b = 0^b = b,这样就实现了a = b;于是,就成功的实现了a,b两个值的交换。
本文出自 “风轻云淡” 博客,请务必保留此出处http://beyond99.blog.51cto.com/1469451/407820
a⊕b = (¬a ∧ b) ∨ (a ∧¬b)
a异或b = (a and (not b) ) or ( (not a) and b)