go - golang 中的 ^0 是什么?

标签 go bitwise-operators twos-complement

我在代码库中看到了 ^0。

例子:

type stat struct {
  ...
  min int64
  ...
}

newStat := stat{min: ^0}

^0 是什么意思?

最佳答案

根据the docs :

^x bitwise complement is m ^ x with m = "all bits set to 1" for

unsigned x and m = -1 for signed x

表示^0与其他主流语言中的~0相同。

two's complement 上(大多数编程语言采用)零补码的值为 -1(在有符号数据类型上)。所以这是一种写法:

newStat := stat{min: -1}

关于go - golang 中的 ^0 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170016/

相关文章:

go - 使用 dep 运行 go vet 时找不到头文件

c++ - 对按位枚举使用 switch case

java - 在Java中,如何模拟 "converting negative numbers casted by unsigned char in c"?

c - 生成位模式

go - golang channel 的分段违规

go - 在 GoLang 中定义独立的标志集

go - 如何在 golang 模板中使用除法?

c - 为什么 gcc 不能正确添加 tmin + tmin?

java - Java 中的 String.format() 和十六进制数字

binary - 什么是 “two' 的补码”?