# Talk:Variable-length quantity

## Concrete

How about making the task more concrete by, say, changing it to a task to show the bits of 12345678901234566789 as binary then as Vlq - mabe grouping into 8 bit octets for readability? Then a function to change back and display the result . --Paddy3118 16:00, 14 October 2010 (UTC)

Done! dingowolf 16:58, 14 October 2010 (UTC)

## Vlq octet order

The wp article states:

VLQ octets are arranged most significant first in a stream.

Which doesn't make sense as the least significant octet has to be sent first so that its eighth bit can be sampled to see if there will be another octet or not. --Paddy3118 15:14, 15 October 2010 (UTC)

I think, in a mixed data stream for storage or transmission, the VLQ has to be like a big ending number , so that the least significant octet act as a terminal, if no other information telling the length of the VLQ. If the stream contain only VLQ, the least significant octet can sent first. dingowolf 16:27, 15 October 2010 (UTC)
What? I read the docs as saying that it is the LSO that has its low bit cleared, making perfect sense as a terminator. –Donal Fellows 13:48, 17 October 2010 (UTC)

I interpret an 8-to-14 bit integer being sent as the first and second bytes being, in order:

VLQ Octet #0
7  6  5  4  3  2  1  0
26 25 24 23 22 21 20
1 Bn
VLQ Octet #1
7  6  5  4  3  2  1  0
213 212 211 210 29 28 27
0 Bn

i.e. the first byte/octet through would have its high bit set and the last octet would not. --Paddy3118 17:17, 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. It is regarding to one bit.

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;
}
```

Andrz (talk) 19:33, 30 March 2024 (UTC)