golang中的“&^”运算符是什么?
What is the "&^" operator in golang?
我真的不能 google 名称 AND NOT 并得到任何有用的结果,这个运算符到底是什么,我怎么能用像 C 这样的语言来做到这一点?我检查了规范,里面没有任何帮助,但有一个列表说它是 &^
(AND NOT)。
Go 表达式 x &^ y
的 C 等价物就是 x & ~y
。字面意思是“x
AND(y
的按位非”)。
在 arithmetic operators section of the spec 中将 &^
描述为 "bit clear" 操作,这让您了解您想要使用它的目的。作为两个独立的操作,~y
会将每个位转换为零,然后清除 x
中的相应位。每个零位将转换为一个,这将保留 x
.
中的相应位
因此,如果您将 x | y
视为一种基于掩码常量 y
打开 x
某些位的方法,那么 x &^ y
则相反并关闭那些相同的位。
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.
示例:
package main
import "fmt"
func main(){
var x uint8 = 1
var y uint8 = 1 << 2
fmt.Printf("%08b\n", x &^ y);
}
结果:
00000001
我真的不能 google 名称 AND NOT 并得到任何有用的结果,这个运算符到底是什么,我怎么能用像 C 这样的语言来做到这一点?我检查了规范,里面没有任何帮助,但有一个列表说它是 &^
(AND NOT)。
Go 表达式 x &^ y
的 C 等价物就是 x & ~y
。字面意思是“x
AND(y
的按位非”)。
在 arithmetic operators section of the spec 中将 &^
描述为 "bit clear" 操作,这让您了解您想要使用它的目的。作为两个独立的操作,~y
会将每个位转换为零,然后清除 x
中的相应位。每个零位将转换为一个,这将保留 x
.
因此,如果您将 x | y
视为一种基于掩码常量 y
打开 x
某些位的方法,那么 x &^ y
则相反并关闭那些相同的位。
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.
示例:
package main
import "fmt"
func main(){
var x uint8 = 1
var y uint8 = 1 << 2
fmt.Printf("%08b\n", x &^ y);
}
结果:
00000001