JSFiddle - React, Tailwind, and code Playground

Variable Byte Binary Number Array

by skibulk

JavaScript

console.clear();
var data = [1, 2, 0, 35, 45, 79];
console.log(data);
var binary = binaryPackIntegers(data, 1);
console.log(binary);
console.log(binaryUnpackIntegers(binary, 1));
console.log(binToBase64(binary));

/*
// Add Delta function
Other options for padding: 3-bit flag, 
If subhead is 1, could let the value be explicit.

Examples must be viewed on a wide screen.
Binary    0000000000000000 0000000000000000 0000000000000000 0000000000000000
Type      A--------------- B--------------- C--------------- D---------------
Head      00XX------------ 01XX------------ 10XX------------ 11XX------------
Subheads  ---------------- ---------------- ----XX---YY----- ----XX-----YY---
Integers  ----XXXXXYYYYY-- ----XX-XX-YY---- ------XXX--YYY-- ------XX-XX--YY-
Repeaters ---------------- ------1--0--0--- ---------------- --------1--0---0
Padding   --------------00 -------------000 --------------00 ----------------

Compresses an array of integers using binary packing.
Optimized for random numbers between 1 and 2^31.

The output starts with a 2-bit global header giving a value from 0 to 3.
This value plus 2 indicates the number of bits in all following subheaders.
A subheader's value minus 1 indicates the number of bits in the
following integer. (We remove the left-most "1" bit from each integer
and let it be implied. We'll put the "1" bits back when we decompress
the data.) If the last byte is fractional, we append 0s to make it whole.
As a result, a subheader of 0b00 must terminates the list. To handle
integers <= 0, an offset must be added to each number before compression
and subtracted from each number after decompression.

For example, in b00110010 (00 10 00 00):
- The global header is 0b00. All subheaders will be 2 (0+2) bits long.
- The first subhead is 0b10. The first integer is 3 bits long (2 + 1 implied).
- The first integer is 0b(1)00, which equals 4.
- The final 0b00 makes a whole byte and terminates the sequence.

Notes:
- A global header of 0b11 (5) allows a...