英文:
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.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论