Pie*_*tti 8 bit-manipulation go
在包中golang.org/x/sys/windows/svc有一个包含此代码的示例:
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
Run Code Online (Sandbox Code Playgroud)
管道|特征是什么意思?
use*_*097 26
正如其他人所说,它是按位[包含] OR运算符.更具体地说,运算符被用于创建位掩码标志,这是一种基于逐位算术组合选项常量的方法.例如,如果你有两个幂的选项常量,如下所示:
const (
red = 1 << iota // 1 (binary: 001) (2 to the power of 0)
green // 2 (binary: 010) (2 to the power of 1)
blue // 4 (binary: 100) (2 to the power of 2)
)
Run Code Online (Sandbox Code Playgroud)
然后你可以将它们与按位OR运算符组合,如下所示:
const (
yellow = red | green // 3 (binary: 011) (1 + 2)
purple = red | blue // 5 (binary: 101) (1 + 4)
white = red | green | blue // 7 (binary: 111) (1 + 2 + 4)
)
Run Code Online (Sandbox Code Playgroud)
因此,它简单地为您提供了一种基于逐位算术组合选项常量的方法,依赖于在二进制数系统中表示2的幂的方式; 注意使用OR运算符时如何组合二进制位.(有关更多信息,请参阅C编程语言中的此示例.)因此,通过组合示例中的选项,您只需允许服务接受停止,关闭和暂停以及继续命令.