英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论