For logical operations in computers, see: https://blog.csdn.net/weixin_37909391/article/details/131441253
&^ bit clear (AND NOT) Bit clearing
Rules for bit clearing operations
- For each bit, if the corresponding bit of the right operand is 0, then the corresponding bit of the result is the same as the left operand.
- For each bit, if the corresponding bit of the right operand is 1, then the corresponding bit of the result is 0, regardless of the corresponding bit of the left operand.
package main
import "fmt"
func main() {
a := 5 // 二进制表示为 0101
b := 3 // 二进制表示为 0011
result := 0
// 按位与
result = a & b // 0001
fmt.Println(result)
// 按位或
result = a | b // 0111
fmt.Println(result)
// 按位非
result = ^a // 1010
fmt.Println(result)
// 按位清零
result = a &^ b // 0100
fmt.Println(result)
// 按位异或
result = a ^ b // 0110
fmt.Println(result)
}