uint(0) – uint(1) 的输出结果是 uint64 的最大值。

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

uint(0) - uint(1) output maxValue of uint64

问题

有人能告诉我为什么输出不是-1而是18446744073709551615吗?

package main

import (
	"fmt"
)

func main() {
	a := uint(0)
	b := uint(1)
	fmt.Println(a - b)
}

输出结果为

18446744073709551615

目前,我的理解如下。

在计算机中,减法会被转换为加法。而b是负数,所以在加法中会使用a(0)和b(-1)的二进制补码。

所以计算过程如下:

00000000 00000000 00000000 00000001 <- 0的二进制补码
11111111 11111111 11111111 11111110 <- -1的二进制补码

11111111 11111111 11111111 11111111 <- (结果:0 + -1)的二进制补码

通常情况下,如果a和b是int类型,二进制补码会自动转换为二进制结果(10000000 00000000 00000000 00000001),即-1。但是a和b是uint类型,所以不会进行二进制转换,它会将11111111 11111111 11111111 11111111(二进制补码)作为普通的二进制结果并输出。

这个解释正确吗?我的理解有什么遗漏吗?

另一个问题是,11111111 11111111 11111111 11111111应该是2的64次方减1,即18446744073709552000。

为什么我的输出只有18446744073709551615?

英文:

Can anybody tell me why the output is not -1 but 18446744073709551615?

package main

import (
	&quot;fmt&quot;
)

func main()  {
	a := uint(0)
	b := uint(1)
	fmt.Println(a - b)
}

output

18446744073709551615

currently, my understanding is the following.

subtraction in pc will be converted into addition. And b is minus, so the 2's complement of a(0) and b(-1) will be used in addition.

so the calculation is like

00000000 00000000 00000000 00000001 &lt;- 2&#39;s complement of 0
11111111 11111111 11111111 11111110 &lt;- 2&#39;s complement of -1

11111111 11111111 11111111 11111111 &lt;- 2&#39;s complement of (result: 0 + -1)

Usually, if a and b is a type of int, 2's complement will be converted to binary result(10000000 00000000 00000000 00000001) which is -1 automatically.
But a and b is the type of unit, so the convert into binary will not be taken, it will treat the result of 11111111 11111111 11111111 11111111(2's complement) as a normal binary result and output it.

← Does this explanation correct?

Does my understanding miss something?

Another question is that
11111111 11111111 11111111 11111111 should be pow(2,64) - 1 which is 18446744073709552000

Why my output is only 18446744073709551615?

答案1

得分: 1

uint64是所有无符号64位整数的集合。范围:0到18446744073709551615。

英文:

> uint64 is the set of all unsigned 64-bit integers. Range: 0 through 18446744073709551615.

答案2

得分: 1

只回答“另一个问题”:

2的64次方减1等于18446744073709551615。

如果你得到的是18446744073709552000,我猜测你可能是从一个使用十进制浮点表示法的计算器上得到的,而且精度不足以准确表示它。

(标准的二进制浮点格式会将18446744073709551616作为最接近的可表示答案。)

另外:2的64次方减1不可能等于18446744073709552000,因为显而易见,2的幂次方是偶数,所以2的N次方减1必定是奇数。

英文:

And just to answer the "another question":

> ... should be pow(2,64) - 1 which is 18446744073709552000
> Why my output is only 18446744073709551615?

2<sup>64</sup> - 1 is 18446744073709551615.

If you're getting 18446744073709552000, my guess would be you're getting it from a calculator that uses a decimal floating-point representation and not enough digits of precision to represent it exactly.

(Standard binary floating point formats would have 18446744073709551616 as the nearest representable answer.)

Aside: 2<sup>64</sup> - 1 could not possibly be equal to 18446744073709552000, because powers of two are even numbers for obvious reasons, so 2<sup>N</sup> - 1 must be odd.

huangapple
  • 本文由 发表于 2021年8月7日 18:33:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/68691550.html
匿名

发表评论

匿名网友

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

确定