gpt4 book ai didi

go - &^ 运算符的作用是什么?

转载 作者:IT王子 更新时间:2023-10-29 01:26:48 25 4
gpt4 key购买 nike

根据specification这个运算符叫做 bit clear:

&^   bit clear (AND NOT)    integers

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

它似乎采用左操作数并禁用右操作数中打开的所有位。是否有运算符(operator)的任何正式描述?我注意到的另一件事是它不是可交换的。与 ^ 比较的伪代码:

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

最佳答案

从符号(&^ 的串联),名称“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 。)

关于go - &^ 运算符的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23979172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com