binaryarray.js

1.1.0 • Public • Published

BinaryArray.js

npm npm bundle size GitHub last commit GitHub Workflow Status

BinaryArray.js is an array of binary bits for JavaScript.

Usage

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 to 0
  • arr.append(bits): appends bits too the array after the last appended bits. Can take a boolean (where true = 1, false = 0), a string (consisting of only 1s and 0s, e.g. "10110101110"), or a number (converted to binary, i.e. 250 = 11111010, 17 = 10001 etc.)
    • arr.appendBits(bits): has the same fucntion.
  • arr.setBitAt(bit, index): sets the bit at index to the value of bit. bit can be either a boolean or a 1 or a 0. Returns the value of the replaced bit.
  • arr.getBitAt(index): returns the bit at the given index, i.e. when running arr.getBitAt(6) on an array with the bits 011011101011000 would result in 1.
  • 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 elemets value, which is the value of the next bit and done, which is true 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 of requestedBits, or the remaining bits in the iterator.
  • iterator.done(): returns true 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.

Readme

Keywords

Package Sidebar

Install

npm i binaryarray.js

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

22.1 kB

Total Files

4

Last publish

Collaborators

  • stelfoog