运算规则
- 0^0=0
- 0^1=1
- 1^0 = 1
- 1^1 = 0
将两个变量的值转化为二进制的, 再按照上边的运算规则进行
利用异或运算交换两个变量的值
方法一:利用Python特性
a = 1
b = 2
a, b = b, a
方法二:异或运算
a = 2 # 10
b = 3 # 11
a = a ^ b # 01
b = b ^ a # 10
a = a ^ b # 11
添加一个go版本的
package main
import "fmt"
/*
a = 1 , b = 0
a = 01 , 00 -> 01
b = 00 , 01 ->01
a = 01 , 01 -> 00
*/
func main() {
var a int = 10
var b int = 9
a = a ^ b
b = b ^ a
a = a ^ b
fmt.Println(a)
fmt.Println(b)
}