以下摘自The Go Programming Language:
The &^ operator is bit clear (AND NOT): in the expression z = x &^ y, each bit of z is 0 if the corresponding bit of y is 1; otherwise it equals the corresponding bit of x.
即z = x &^ y
运算相当于先把y
取反(针对y
的每个bit
:0
变成1
,1
变成0
),然后再和x
进行&
运算。参考下例:
package main
import "fmt"
func main(){
var x uint8 = 1
var y uint8 = 1 << 2
fmt.Printf("%08b\n", x &^ y);
}
执行结果如下:
00000001