gpt4 book ai didi

go - uint64 是否需要 8 个字节的存储空间?

转载 作者:IT老高 更新时间:2023-10-28 13:10:27 24 4
gpt4 key购买 nike

官方文档说uint64是一个64位的无符号整数,是不是说任何一个uint64数都应该占用8个字节的存储空间,不管它有多大?

编辑:

谢谢大家的回答!

当我注意到 binary.PutUvarint 消耗多达 10 个字节来存储一个大的 uint64 时,我提出了疑问,尽管最大 uint64应该只占用 8 个字节。

然后我在 Golang lib 的源代码中找到了答案:

Design note:
// At most 10 bytes are needed for 64-bit values. The encoding could
// be more dense: a full 64-bit value needs an extra byte just to hold bit 63.
// Instead, the msb of the previous byte could be used to hold bit 63 since we
// know there can't be more than 64 bits. This is a trivial improvement and
// would reduce the maximum encoding length to 9 bytes. However, it breaks the
// invariant that the msb is always the "continuation bit" and thus makes the
// format incompatible with a varint encoding for larger numbers (say 128-bit).

最佳答案

根据http://golang.org/ref/spec#Size_and_alignment_guarantees :

type                                 size in bytes

byte, uint8, int8 1
uint16, int16 2
uint32, int32, float32 4
uint64, int64, float64, complex64 8
complex128 16

所以,是的,uint64总是占用 8 个字节。

关于go - uint64 是否需要 8 个字节的存储空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com