What is ^0 in golang?

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

What is ^0 in golang?

问题

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

示例:

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

newStat := stat{min: ^0}

^0 是什么意思?

英文:

I am seeing ^0 in the code base.

Example:

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

newStat := stat{min: ^0}

What does ^0 mean?

答案1

得分: 27

根据文档

> ^x 按位取反 对于无符号 x,m = “所有位都设置为1”,对于有符号 x,m = -1

因此,^x 反转 x 中的每个位,例如 0101 变为 1010。这意味着 ^0 在其他主流语言中与 ~0 相同。

当使用二进制补码表示负数(大多数编程语言都是如此)时,按位取反的零(所有位都为1)的值为-1。因此,这是一种写法:

newStat := stat{min: -1}
英文:

According to the docs:

> ^x bitwise complement is m ^ x with m = "all bits set to 1" for
>
> unsigned x and m = -1 for signed x

So ^x inverts each of the bits in x, eg. 0101 becomes 1010. This means that ^0 is the same as ~0 in other mainstream languages.

When using two's complement to represent negative numbers (which most programming languages do), the value of the bitwise complement of zero (where all bits are 1) is -1. So this is a way to write:

newStat := stat{min: -1}

huangapple
  • 本文由 发表于 2013年10月4日 06:05:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/19170016.html
匿名

发表评论

匿名网友

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

确定