Usage and meaning of &^ and &^= operators in Go

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

Usage and meaning of &^ and &^= operators in Go

问题

我已经寻找了大约一周,但似乎找不到关于Go语言中这两个运算符&^&^=的合理解释以及它们的使用方法。是否有人可以友好地给我解释一下呢?

英文:

I've been looking around for about a week now and can't seem to find a decent explanation for these two operators, &^ and &^=, in the Go language and how they are used. Would anybody be as kind as to enlighten me?

答案1

得分: 11

这在我们查看所有位运算符时更容易理解:

&    按位与
|    按位或
^    按位异或
&^   按位清除(与非)
  1. 按位与(&):当两个操作数的位都为1时,结果为1,否则结果为0。
  2. 按位或(|):当至少一个操作数的位为1时,结果为1,否则如果两个操作数的位都为0,则结果为0。
  3. 按位异或(^):当且仅当一个操作数的位为1时,结果为1,否则结果为0。这三个运算符(&,|,^)无论操作数的顺序如何,都会产生相同的结果。
  4. 按位与非(&^):当第一个操作数的位为1且第二个操作数的位为0时,结果为1,否则结果为0。注意,操作数的顺序会影响结果。要使结果为1,第一个操作数的位必须为1,第二个操作数的位必须为0。

以下是演示位运算符行为的代码,也可以在Go Playground上找到:

package main

import "fmt"

func main() {
	fmt.Println(`AND`)
	fmt.Printf("%b & %b 的结果是 %03b\n", 4, 5, 4&5)
   	fmt.Printf("%b & %b 的结果是 %03b\n", 5, 4, 5&4)
   	fmt.Println(`OR`)
	fmt.Printf("%b | %b 的结果是 %03b\n", 4, 5, 4|5)
    fmt.Printf("%b | %b 的结果是 %03b\n", 5, 4, 5|4)
 	fmt.Println(`XOR`)
	fmt.Printf("%b ^ %b 的结果是 %03b\n", 4, 5, 4^5)
	fmt.Printf("%b ^ %b 的结果是 %03b\n", 5, 4, 5^4)
	fmt.Println(`AND NOT`)
 	fmt.Printf("%b &^ %b 的结果是 %03b\n", 7, 5, 7&^5)
	fmt.Printf("%b &^ %b 的结果是 %03b\n", 5, 7, 5&^7)
}

运行上述代码生成的输出为:

AND
100 & 101 的结果是 100
101 & 100 的结果是 100
OR
100 | 101 的结果是 101
101 | 100 的结果是 101
XOR
100 ^ 101 的结果是 001
101 ^ 100 的结果是 001
AND NOT
111 &^ 101 的结果是 010
101 &^ 111 的结果是 000

最后,&^= 是一个简写的赋值运算符。例如,x = x &^ y 可以替换为 x &^= y

英文:

This is easier to understand when we take a look at all the bitwise operators:

&    bitwise AND
|    bitwise OR
^    bitwise XOR
&^   bit clear (AND NOT)
  1. Bitwise AND (&): Result is 1 when both operand bits are 1, else the result is 0.
  2. Bitwise OR (|): Result is 1 when at least one operand bit is 1, else 0 if both operand bits are 0.
  3. Bitwise XOR (^): Result is 1 when one, and only one operand bit is 1, else the result is 0. These three operators (&, |, ^) produce the same result irrespective of the order of operand bits.
  4. Bitwise AND NOT (&^): Result is 1 when the first operand bit is 1, and the second operand bit is 0; else the result is 0. Note that the order of the operand bit affects the result. For the result to be 1, the first operand bit must be 1 and the second must be 0.

Here's code, also on the Go Playground, that demonstrates the behavior of bitwise operators:

package main

import "fmt"

func main() {
	fmt.Println(`AND`)
	fmt.Printf("%b & %b results in %03b\n", 4, 5, 4&5)
   	fmt.Printf("%b & %b results in %03b\n", 5, 4, 5&4)
   	fmt.Println(`OR`)
	fmt.Printf("%b | %b results in %03b\n", 4, 5, 4|5)
    fmt.Printf("%b | %b results in %03b\n", 5, 4, 5|4)
 	fmt.Println(`XOR`)
	fmt.Printf("%b ^ %b results in %03b\n", 4, 5, 4^5)
	fmt.Printf("%b ^ %b results in %03b\n", 5, 4, 5^4)
	fmt.Println(`AND NOT`)
 	fmt.Printf("%b &^ %b results in %03b\n", 7, 5, 7&^5)
	fmt.Printf("%b &^ %b results in %03b\n", 5, 7, 5&^7)
}

The output generated by running the above code is:

AND
100 & 101 results in 100
101 & 100 results in 100
OR
100 | 101 results in 101
101 | 100 results in 101
XOR
100 ^ 101 results in 001
101 ^ 100 results in 001
AND NOT
111 &^ 101 results in 010
101 &^ 111 results in 000

And finally, &^= is a shorthand assignment operator. For example, x = x &^ y can be replaced by x &^= y

答案2

得分: 1

根据规范,它们是位清除运算符:

&^   位清除(AND NOT)    整数

你可以将它们用作位标志值的一部分。使用or来打开一个位,使用and not来关闭它。

英文:

The spec says that they are the bit clear operators:

&^   bit clear (AND NOT)    integers

You would use them as part of a bit flag value. You'd use or to turn on a bit, and and not to turn it off.

huangapple
  • 本文由 发表于 2014年11月24日 11:32:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/27097461.html
匿名

发表评论

匿名网友

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

确定