Talk:Variable-length quantity: Difference between revisions
→Is possible only 9 bytes for full 64 bits: new section
(→Is possible only 9 bytes for full 64 bits: new section) |
|||
Line 61:
I agree with Paddy. See also: http://en.wikipedia.org/wiki/File:Uintvar_coding.svg --[[User:Rdm|Rdm]] 23:07, 17 October 2010 (UTC)
== Is possible only 9 bytes for full 64 bits ==
VLQ uses 7 bits on byte. 7*9 = 63 but fortunately last (==8) byte not need flag; although can have set high bit, is possible to stop counting bytes.
My methods:
int number_len(uint64_t x) {
int k = 0;
while (x > 127 && k < 8) {
k++;
x >>= 7;
}
return k + 1;
}
void to_seq(uint64_t x, uint8_t *out) {
int k = 0;
while (x > 127 && k < 8) {
out[k] = (uint8_t) x | 128;
k++;
x >>= 7;
}
out[k] = (uint8_t) x;
}
uint64_t from_seq(const uint8_t *in) {
uint64_t r = 0;
int n = 0;
while (n < 8 && *in & 128) {
r |= ((uint64_t) (*in & 127)) << (n * 7);
n++;
in++;
}
r |= ((uint64_t) (*in)) << (n * 7);
return r;
}
[[User:Andrz|Andrz]] ([[User talk:Andrz|talk]]) 19:33, 30 March 2024 (UTC)
|