PHPackages                             calamity-inc/u64-dyn - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. calamity-inc/u64-dyn

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

calamity-inc/u64-dyn
====================

Variable-length 64-bit integer codings that take at most 9 bytes.

111[1 issues](https://github.com/calamity-inc/u64_dyn/issues)CCI passing

Since Oct 30Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/calamity-inc/u64_dyn)[ Packagist](https://packagist.org/packages/calamity-inc/u64-dyn)[ RSS](/packages/calamity-inc-u64-dyn/feed)WikiDiscussions senpai Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Variable-length 64-bit integer codings that take at most 9 bytes.

- Unsigned
    - [u64\_dyn](#u64_dyn)
    - [u64\_dyn\_b](#u64_dyn_b) *(fka. `u64_dyn_v2`)*
    - [u64\_dyn\_p](#u64_dyn_p)
    - [u64\_dyn\_bp](#u64_dyn_bp)
- Signed
    - [i64\_dyn\_a](#i64_dyn_a) *(fka. `i64_dyn`)*
    - [i64\_dyn\_b](#i64_dyn_b) *(fka. `i64_dyn_v2`)*
    - [i64\_dyn\_bp](#i64_dyn_bp)

### Guide

[](#guide)

- Biased variants (indicated with a `b`) need less bytes to encode certain sequences.
- Prefixed variants (indicated with a `p`) are a lot faster to read due to the first byte indicating the sequence length.
- For signed codings, there is also `a` to indicate arithmetic negation as opposed to bitwise negation.

Implementations
---------------

[](#implementations)

- [C](c)
- C++
    - [Soup](https://github.com/calamity-inc/Soup) has implementations for [reading](https://github.com/calamity-inc/Soup/blob/senpai/soup/Reader.cpp) and [writing](https://github.com/calamity-inc/Soup/blob/senpai/soup/Writer.cpp) — these are vectorized if supported by the CPU
- [JavaScript](js)
- [Lua](lua/u64_dyn.lua)
    - The [APM](https://github.com/PlutoLang/apm#readme) package for this can be found [here](https://gist.github.com/Sainan/02c3ac9cea5015341412c92feec95e56)
- [PHP](php)
- [Rust](rust)

Specifications
--------------

[](#specifications)

### u64\_dyn

[](#u64_dyn)

The first 56 bits of the integer are encoded in up to eight little-endian 7-bit groups where the most significant bit indicates if another byte follows. The final 8 bits are encoded as-is.

ValueEncoded As`0x7f``7f``0x80``80 01``0x4000``80 80 01``0xffffffffffffffff``ff ff ff ff ff ff ff ff ff`### u64\_dyn\_b

[](#u64_dyn_b)

Same as [u64\_dyn](#u64_dyn) but for each continuation bit (i.e., "another byte follows"), 1 is subtracted from the remaining value after shifting. Decoding compensates by adding a bias.

ValueEncoded As`0x7f``7f``0x80``80 00``0x4000``80 7f``0xffffffffffffffff``ff fe fe fe fe fe fe fe fe`Contrived sequences which would exceed 2^64 (e.g. `ff ff fe fe fe fe fe fe fe`) are illegal. Decoders can detect them (by checking `v > UINT64_MAX - bias` before adding the bias) and should return an error appropriately.

### u64\_dyn\_p

[](#u64_dyn_p)

Same as [u64\_dyn](#u64_dyn) but the continuation bits are all moved to the first byte:

```
0xxxxxxx
10xxxxxx xxxxxxxx
110xxxxx xxxxxxxx xxxxxxxx
1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
11110xxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
111110xx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
1111110x xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
11111110 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
11111111 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx

```

Prefixing does not change the length of coded values.

ValueEncoded As`0x7f``7f``0x80``80 02``0x4000``C0 80 02``0xffffffffffffffff``ff ff ff ff ff ff ff ff ff`### u64\_dyn\_bp

[](#u64_dyn_bp)

This coding uses the biasing of [u64\_dyn\_b](#u64_dyn_b) and the prefixing of [u64\_dyn\_p](#u64_dyn_p).

ValueEncoded As`0x7f``7f``0x80``80 00``0x4000``80 fe``0xffffffffffffffff``ff 7f bf df ef f7 fb fd fe`### i64\_dyn\_a

[](#i64_dyn_a)

The i64 value is split into (neg, u63) like so:

```
neg := i64 < 0
if neg:
    u63 := (~i64 + 1) & ~(1
