Home

Awesome

bit-array

NPM version build status npm download

Bit-array operations in JavaScript.

Installation

npm install ml-bit-array

Methods

All exported methods are static and do not change the original array unless indicated otherwise.
Numbers in array arguments are treated as 32-bit signed integers.
The library is designed with speed in mind so argument type and length are not checked.

count(arr)

Computes the amount of 1s in the array. This is also known as Hamming weight.

and(arr1, arr2)

Computes the logical AND operation and returns the result in a new array.

or(arr1, arr2)

Computes the logical OR operation and returns the result in a new array.

xor(arr1, arr2)

Computes the logical XOR operation and returns the result in a new array.

not(arr)

Computes the logical NOT operation and returns the result in a new array.

getBit(arr, n)

Returns true if the bit at position n is 1, false if it is 0.

Imagine that you have an array of 4-bit numbers like this ['0001', '1010'], the 0th position will be 0 because it is the most significant bit of the 0th element of the array, and the 4th position will be 1, because will be the most significant bit in the 1st element of the array (remember that the true number of bits for a number in this case is 32).

setBit(arr, n, val)

Sets the bit at position n to 1 if val is a truthy value, otherwise sets it to 0.

toBinaryString(arr)

Converts an array of numbers to a string representation of the bits, so toBinaryString([1]) will return '00000000000000000000000000000001'.
The length of the string will be arr.length * 32.

parseBinaryString(str)

Converts a string representation of bits to an array, so parseBinaryString('00000000000000000000000000000010') will return [2].
This is the exact inverse of toBinaryString.

toHexString(arr)

Converts an array of numbers to a hexadecimal representation of the bits, so toHexString([-1]) will return 'ffffffff'.
The length of the string will be arr.length * 8.

parseHexString(str)

Converts a hexadecimal representation of bits to an array, so parseHexString('00000010ffff0000') will return [16, -65536].
This is the exact inverse of toHexString.

toDebug(arr)

Returns a human-readable string from the array in the format:

0000: 0000 1000 1111 1000 0011 1101 1111 0001
0020: 0000 1000 1111 1000 0011 1101 1111 0001
0040: 0000 1000 1111 1000 0011 1101 1111 0001

Authors

License

MIT