一个uint64占用8个字节的存储空间吗?

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

Does a uint64 take 8 bytes storage?

问题

官方文件表示uint64是一个64位的无符号整数,这是否意味着任何uint64数字都应该占用8个字节的存储空间,无论它有多小或多大?

编辑:

感谢大家的回答!

当我注意到binary.PutUvarint在存储一个大的uint64时需要最多10个字节时,我提出了这个疑问,尽管最大的uint64只需要8个字节。

然后我在Golang库的源代码中找到了对我的疑问的答案:

设计说明:
// 64位值最多需要10个字节。编码可以更紧凑:一个完整的64位值需要额外的一个字节来保存第63位。
// 相反,前一个字节的最高有效位可以用来保存第63位,因为我们知道最多只有64位。这是一个微不足道的改进,
// 可以将最大编码长度减少到9个字节。然而,它打破了最高有效位始终是“连续位”的不变性,因此使得该格式与更大的数字(比如128位)的varint编码不兼容。

英文:

Official document says uint64 is an unsigned integer of 64-bits, does that mean any uint64 number should take 8 bytes storage, no matter how small or how large it is?

Edit:

Thanks for everyone's answer!

I raised the doubt when I noticed that binary.PutUvarint consumes up to 10 bytes to store a large uint64, despite that maximum uint64 should only take 8 bytes.

I then found answer to my doubt in the source code of 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).

答案1

得分: 15

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

类型                                 字节大小

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

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

英文:

According to 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

So, yes, uint64 will always take 8 bytes.

答案2

得分: 1

简单来说:是的,一个64位的固定大小整数类型总是占用8个字节。除非是一种不寻常的语言,否则情况都是如此。

有些语言/平台支持可变长度的数字类型,其中内存中的存储确实取决于值,但在这种简单的情况下,你不会指定类型中的位数,因为它可能会有所不同。

英文:

Simply put: yes, a 64-bit fixed size integer type will always take 8 bytes. It would be an unusual language where that isn't the case.

There are languages/platforms which support variable-length numeric types where the storage in memory does depend on the value, but you wouldn't then specify the number of bits in the type in such a simple way, as that can vary.

答案3

得分: 1

Go编程语言规范

数值类型

数值类型表示整数或浮点数值的集合。
预定义的与体系结构无关的数值类型有:

uint64      所有无符号64位整数的集合(从0到18446744073709551615)

是的,确切地说是64位或8字节。

英文:

> The Go Programming Language Specification
>
> Numeric types
>
> A numeric type represents sets of integer or floating-point values.
> The predeclared architecture-independent numeric types are:
>
> uint64 the set of all unsigned 64-bit integers (0 to 18446744073709551615)

Yes, exactly 64 bits or 8 bytes.

答案4

得分: 1

因此,64位 = 8字节

英文:

Just remember the simple rule, the variable type is usually optimized to fit certain memory space and the minimum memory space is 1 bit(s). And 8 bit(s) = 1 byte(s):

Therefore 64bit(s) = 8 byte(s)

huangapple
  • 本文由 发表于 2013年6月25日 13:38:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/17289898.html
匿名

发表评论

匿名网友

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

确定