BinaryArray.js is an array of binary bits for JavaScript.
The following methods are provided:
-
const arr = new BinaryArray(targetLength)
: creates a new array of the given length (8-aligned) where every bits starts set to0
-
arr.append(bits)
: appends bits too the array after the last appended bits. Can take aboolean
(wheretrue = 1
,false = 0
), astring
(consisting of only1
s and0
s, e.g."10110101110"
), or anumber
(converted to binary, i.e.250
=11111010
,17
=10001
etc.)-
arr.appendBits(bits)
: has the same fucntion.
-
-
arr.setBitAt(bit, index)
: sets the bit atindex
to the value ofbit
.bit
can be either aboolean
or a1
or a0
. Returns the value of the replaced bit. -
arr.getBitAt(index)
: returns the bit at the given index, i.e. when runningarr.getBitAt(6)
on an array with the bits011011101011000
would result in1
. -
const iterator = arr.iterator()
returns an iterator for the array over the array as it stands when created. Only bits set with the append method are iterated over (including bits set manually over already appended bits).-
arr.iterator(true)
: gives an iterator that can iterate over the total length of the array regardless of how many bits have been appended.
-
-
arr.length
: gives the real length (i.e. 8-aligned) of the array.-
arr.size
: gives the same value.
-
-
arr.bitsSet
: returns how many bits have been appended to the array.
Iterator methods:
-
iterator.next()
: returns an object consisting of the elemetsvalue
, which is the value of the next bit anddone
, which istrue
if the last set bit has been taken. -
iterator.nextBit()
: returns the value of the next bit -
iterator.nextBits(requestedBits)
: returns the next n bits, were n is the value ofrequestedBits
, or the remaining bits in the iterator. -
iterator.done()
: returnstrue
if there are no more bits to read in the iterator. -
iterator.reset()
: resets the iterator so all bits can be gone though again. -
iterator.bitsLeft
: returns the bits left to go though in the iterator.