What does the &^ operator do?

huangapple go评论83阅读模式
英文:

What does the &^ operator do?

问题

根据规范,这个运算符被称为位清除运算符:

&^   位清除 (AND NOT)    整数

我以前从未听说过这样的运算符,我想知道它有什么用处。

它似乎会将左操作数中打开的所有位都禁用。这个运算符有没有正式的描述?

我还注意到它不是可交换的。
^进行比较的伪代码:

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
英文:

According to the specification this operator is called 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

答案1

得分: 6

从符号(&^的连接)以及名称“and not”(还有听起来像“bit clear”的术语,它似乎是“bit set”的相反),可以明显看出A &^ B执行的是A & ^B(其中^是按位取反)。

这一点可以通过查看运算符的真值表来证实:

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

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

英文:

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).

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.)

huangapple
  • 本文由 发表于 2014年6月1日 19:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/23979172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定