Node BitView
NodeJS Buffer interface provides a way to manipulate bytes, but what if we needed to manipulate bits?
This library was created to address the gap of a built-in bit interface.
Installation
npm install bitview --save
Initializing a view
new BitView(Length)
new BitView(buffer)
new BitView(buffer, byteOffset, length);
Buffer.from(arg)
Parameters:
length: length in bits
buffer: use an existing buffer
arg: Another interface to create a new BitView instance
arg could be an array of bits [1,0,1] or a string '1010' or a buffer.
byteOffset: default 0, starting offset of the bitview.
Methods
.flip(pos)
flips the value of the bit at location pos
.get(pos)
Returns the value of the bit at location pos (0 or 1)
.set(pos,v)
Sets the value of a bit at location pos to v
.toString()
Returns a string representation of the BitView.
.toBuffer()
Returns the buffer used by the view.
Properties
.length
Returns length in bits.
Usage:
const BitView = const view= 10; view;console; // trueviewconsole; // false const view2=BitView;console; // true const view3=BitView;console; // falseconsole; // true
Implementation
BitView uses bitwise operations to manipulate each bit in a buffer (Low Complexity)