dist/utils/BitArray.js
function arrayToBitArray(numberArray, bufferLength, destinationBuffer) {
    const returnBuffer = destinationBuffer ? destinationBuffer : Buffer.alloc(bufferLength);
    if (destinationBuffer) {
        // Get the bufferLength from the destination buffer, if one is provided
        bufferLength = destinationBuffer.byteLength;
    }
    const maxAllowedNumber = bufferLength * 8 - 1; // Max. allowed number is defined by the buffer size
    // Fill buffer with zeros first
    returnBuffer.fill(0);
    // Write bits
    for (let index = 0; index < numberArray.length; index++) {
        // Check for valid number
        const numberToWrite = numberArray[index];
        if (numberToWrite < 0 || numberToWrite > maxAllowedNumber)
            throw new Error("Number out of range.");
        // Set bit
        returnBuffer[Math.floor(numberToWrite / 8)] |= 1 << (numberToWrite % 8); // numberToWrite / 8 = byte position (0-bufferLength), numberToWrite % 8 = bit position (0-7)
    }
    return returnBuffer;
}
function arrayToBitArray(numberArray, bufferLength, destinationBuffer) {
    const returnBuffer = destinationBuffer ? destinationBuffer : Buffer.alloc(bufferLength);
    if (destinationBuffer) {
        // Get the bufferLength from the destination buffer, if one is provided
        bufferLength = destinationBuffer.byteLength;
    }
    const maxAllowedNumber = bufferLength * 8 - 1; // Max. allowed number is defined by the buffer size
    // Fill buffer with zeros first
    returnBuffer.fill(0);
    // Write bits
    for (let index = 0; index < numberArray.length; index++) {
        // Check for valid number
        const numberToWrite = numberArray[index];
        if (numberToWrite < 0 || numberToWrite > maxAllowedNumber)
            throw new Error("Number out of range.");
        // Set bit
        returnBuffer[Math.floor(numberToWrite / 8)] |= 1 << (numberToWrite % 8); // numberToWrite / 8 = byte position (0-bufferLength), numberToWrite % 8 = bit position (0-7)
    }
    return returnBuffer;
}
function arrayToBitArray ( numberArray , bufferLength , destinationBuffer ) { const returnBuffer = destinationBuffer ? destinationBuffer : Buffer . alloc ( bufferLength ) ; if ( destinationBuffer ) { // Get the bufferLength from the destination buffer, if one is provided bufferLength = destinationBuffer . byteLength ; } const maxAllowedNumber = bufferLength * 8 - 1 ; // Max. allowed number is defined by the buffer size // Fill buffer with zeros first returnBuffer . fill ( 0 ) ; // Write bits for ( let index = 0 ; index < numberArray . length ; index ++ ) { // Check for valid number const numberToWrite = numberArray [ index ] ; if ( numberToWrite < 0 || numberToWrite > maxAllowedNumber ) throw new Error ( "Number out of range." ) ; // Set bit returnBuffer [ Math . floor ( numberToWrite / 8 ) ] |= 1 << ( numberToWrite % 8 ) ; // numberToWrite / 8 = byte position (0-bufferLength), numberToWrite % 8 = bit position (0-7) } return returnBuffer ; }
Converts an array of numbers to a binary bit array.

@export
@param {number[]} numberArray Each number in the array corresponds to the bit that has to be set in the buffer.
@param {number} bufferLength Length of the resulting buffer. This value will be ignored, if a destination buffer is provided.
@param {Buffer} [destinationBuffer] Instead of creating a new buffer, the result can be written directly to an existing buffer.
@returns {Buffer} Returns a new buffer with the bit array or the destination buffer, if a value for the destination buffer is provided.
Converts an array of numbers to a binary bit array .