go中有一个 &^ 的运算符,它代表的是按位置零
首先来看下几个输出例子:
i := 1 &^ 0 fmt.Println("1 &^ 0 -- ",i) i = 1 &^ 1 fmt.Println("1 &^ 1 -- ",i) i = 0 &^ 1 fmt.Println("0 &^ 1 -- ",i) i = 0 &^ 0 fmt.Println("0 &^ 0 -- ",i) fmt.Println("") j := 2 &^ 0 fmt.Println("2 &^ 0 -- ",j) j = 2 &^ 2 fmt.Println("2 &^ 2 -- ",j) j = 0 &^ 2 fmt.Println("0 &^ 2 -- ",j) j = 0 &^ 0 fmt.Println("0 &^ 0 -- ",j)
输出结果为:
1 &^ 0 -- 1
1 &^ 1 -- 0
0 &^ 1 -- 0
0 &^ 0 -- 0
2 &^ 0 -- 2
2 &^ 2 -- 0
0 &^ 2 -- 0
0 &^ 0 -- 0
可以看出。结果是由右边的数值决定的。
结论:
z = x &^ y
如果y非零,则z为0
如果y为零,则z为x