/
main.js
60 lines (48 loc) · 1.97 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Converts an array of X-bit unsigned integers to an array of Y-bit unsigned integers.
*
* @param {integer} Xbit - Number of bits each integer in the input array has
* @param {integer} Ybit - Number of bits each integer in the output array should have
* @param {integer[]} inputArray - Array of X-bit unsigned integers to convert
* @param {integer} [maxOutputLength] - Optional param to limit length of output array
*
* @return {integer[]} array of Y-bit unsigned integers
*/
function convertXtoYbitarray(Xbit, Ybit, inputArray, maxOutputLength=undefined) {
const output = [];
if (maxOutputLength < 1) return output;
let currentIndex = 0;
let bufferBitLength = 0;
let bufferNumber = 0;
while(currentIndex < inputArray.length || bufferBitLength !== 0) {
let currentBitLength = 0;
let currentNumber = 0;
// add the buffer bits to the current bits until it reaches the desired bit length
while (currentBitLength < Ybit) {
// read in a new number into buffer bits
if (bufferBitLength === 0) {
if (currentIndex < inputArray.length) {
bufferNumber = inputArray[currentIndex];
bufferBitLength = Xbit;
currentIndex++;
} else { // out of numbers to read...
bufferNumber = 0;
bufferBitLength = Ybit - currentBitLength;
}
}
// get the bits from the buffer that we'll add to the current bits
const addBitLength = Math.min(Ybit - currentBitLength, bufferBitLength);
const addNumber = bufferNumber >>> (bufferBitLength - addBitLength);
// remove the extracted bits from the buffer bits
bufferBitLength = bufferBitLength - addBitLength;
bufferNumber = bufferNumber ^ (addNumber << bufferBitLength);
// add the bits to the current bits
currentNumber = (currentNumber << addBitLength) + addNumber;
currentBitLength = currentBitLength + addBitLength;
}
output.push(currentNumber);
if (maxOutputLength && output.length >= maxOutputLength) return output;
}
return output;
}
module.exports = convertXtoYbitarray;