官方文檔說 uint64 是一個 64 位的無符號整數,這是否意味著任何 uint64 數字都應該占用 8 個字節的存儲空間,無論它有多小或多大?編輯:謝謝大家的回答!當我注意到binary.PutUvarint最多消耗 10 個字節來存儲一個 large 時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).
uint64 是否需要 8 個字節的存儲空間?
慕的地8271018
2021-06-10 14:08:40