Wondering how you can take an array of small numbers that represent a larger number, and convert it into that larger number, and vice versa.
I'm not really sure how to actually represent this, but the gist of it is as follows.
Say you have a bunch of 4-bit numbers:
[ 3, 2, 5, 13, 2, 8, 1, ... ]
Wondering how to calculate the final number. Not really sure what I mean totally, but what I'm trying to get at is... I don't want to just add the numbers:
3 + 2 + 5 + 13 + 2 + 8 + 1 = 34
And I don't want to just merge the bits.
00000011 + 00000010 + 00000101 + ...
(3) + (2) + (5) + ...
=
000000110000001000000101
I instead want to reverse the following process. Say I have a large number like 2147483650
(slightly more than a 32-bit integer). I want to store that integer in an array of 4-bit chunks. Maybe those chunks look like this:
[ 3, 2, 5, 13, 2, 8, 1, ... ]
So that's the main goal. How to:
- Convert a large number into an array of smaller numbers.
- How to convert the array of small numbers into a single large number.
But maybe the large integer is 32 bits, and the small integers are 2 bits, or maybe they are 8 bits, etc. Basically a generic way to say:
function divideLargeIntoArrayOfSmall(integer, smallBitSize) {
if (smallBitSize == 4) {
return integer.splitIntoBitChunksOf(4)
} else if (smallBitSize == 2) {
return integer.splitIntoBitChunksOf(2)
} else if (smallBitSize == 8) {
return integer.splitIntoBitChunksOf(8)
} else if (smallBitSize == 16) {
return integer.splitIntoBitChunksOf(16)
}
}
function composeIntoSingleLarge(array, itemBitSize) {
if (itemBitSize == 4) {
// array.sum(integerFromBitSize(4))
} else if (itemBitSize == 2) {
// array.sum(integerFromBitSize(2))
} else if (itemBitSize == 8) {
// array.sum(integerFromBitSize(8))
} else if (itemBitSize == 16) {
// array.sum(integerFromBitSize(16))
}
}