Bit-array operations in JavaScript.
npm install ml-bit-array
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.
Computes the amount of 1
s in the array. This is also known as Hamming weight.
Computes the logical AND operation and returns the result in a new array.
Computes the logical OR operation and returns the result in a new array.
Computes the logical XOR operation and returns the result in a new array.
Computes the logical NOT operation and returns the result in a new array.
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).
Sets the bit at position n
to 1 if val
is a truthy value, otherwise sets it to 0.
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
.
Converts a string representation of bits to an array, so parseBinaryString('00000000000000000000000000000010')
will return [2]
.
This is the exact inverse of toBinaryString
.
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
.
Converts a hexadecimal representation of bits to an array, so parseHexString('00000010ffff0000')
will return [16, -65536]
.
This is the exact inverse of toHexString
.
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