简体   繁体   English

&^运算符有什么作用?

[英]What does the &^ operator do?

According to the specification this operator is called bit clear: 根据规范,这个运算符被称为bit clear:

&^   bit clear (AND NOT)    integers

I've never heard of such an operator before, and I'm wondering why is it useful. 我以前从未听说过这样的操作员,我想知道它为什么有用。

It seems to take the left operand and disables all the bits that are turned on in the right operand. 它似乎采用左操作数并禁用右操作数中打开的所有位。 Is there any formal description of the operator? 是否有关于运营商的正式描述? One more thing I noticed is that it's not commutative. 我注意到的另一件事是它不是可交换的。 Pseudocode in comarison with ^ : ^伪代码:

11110 &^ 100 //11010
11110  ^ 100 //11010

11110 &^ 0 //11110
11110  ^ 0 //11110

11110 &^ 11110 //0
11110  ^ 11110 //0

11110 &^ 111 //11000
11110  ^ 111 //11001

111 &^ 11110 //1
111  ^ 11110 //11001

From the symbol (a concatenation of & and ^ ), the name "and not" (and also the term "bit clear" which sounds like the opposite of "bit set"), it seems evident that A &^ B is doing A & ^B (where ^ is the bitwise inverse). 从符号( &^的串联),名称“和不”(以及“bit clear”一词听起来与“bit set”相反),似乎A &^ B正在做A & ^B (其中^是按位反转)。

This is backed up by examining the operator's truth table: 通过检查运算符的真值表来备份:

fmt.Println(0 &^ 0);    // 0
fmt.Println(0 &^ 1);    // 0
fmt.Println(1 &^ 0);    // 1
fmt.Println(1 &^ 1);    // 0

(See http://ideone.com/s4Pfe9 .) (见http://ideone.com/s4Pfe9 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM