Is there a way to store an arbitrarily big BigInt in a bit sequence, only later to convert it into a standard BigInt structure?


I am trying to imagine a way of encoding a BigInt into a bit stream, so that it is literally just a sequence of bits. Then upon decoding this bit stream, you would generate the standard BigInt sort of data structure (array of small integers with a sign). How could you encode the BigInt as a sequence of bits, and how would you decode it? I don’t see how to properly perform the bitwise manipulations or how to encode an arbitrary number in bits larger than 32 or 64. If a language is required then I would be doing this in JavaScript.

For instance, this takes bytes and converts it into a single bit stream:

function arrayOfBytesTo32Int(map) {   return map[0] << 24     | map[1] << 16     | map[2] << 8     | map[3] } 

How would you do that same sort of thing for arbitrarily long bit sequences?