->VARINT
conversion binaryThe ->VARINT
function encodes a LONG
or a list of LONG
s using VarInt encoding. The result is a byte array containing the concatenation of the encoded numbers.
The ->VARINT
function is optimized for encoding unsigned LONG
s, this means that negative numbers (with the most significant bit set to 1) will be encoded on 10 bytes. In order to reduce this footprint you can pre-process the numbers to encode so they are encoded using ZigZag VarInt encoding. The simple trick is to compute
$value 1 << $value 63 >> ^
this will have the effect of alternatively encoding positive and negative numbers thus leading to a more efficient footprint for negative numbers.
At decoding time using VARINT->
, simply undo the Zig-Zag trick:
$unsigned 63 << 63 >> $unsigned ^ 1 >>
// Flip the top bit
$unsigned 1 63 << & ^