Flexible byte representation for JavaScript.
Install via npm:
npm install --save @zekfad/bitbyte
Install via yarn:
yarn add @zekfad/bitbyte
const BitByte = require('@zekfad/bitbyte');
const myByte = new BitByte(200);
console.log(myByte.toString()); // 11001000
console.log([...myByte]); // [ 1, 1, 0, 0, 1, 0, 0, 0 ]
console.log(myByte[1], myByte[2]); // 1 0
myByte[1] = 0;
myByte[2] = 1;
console.log(myByte.toString()); // 10101000
console.log(0 + myByte); // 168
const myByte = new BitByte(200);
console.log(myByte.toString()); // 11001000
const myByte = new BitByte([ 1, 1, 0, 0, 1, 0, 0, 0]);
console.log(myByte.toString()); // 11001000
You can also pass in Booleans:
const myByte = new BitByte([ true, true, false, false, true, false, false, false]);
console.log(myByte.toString()); // 11001000
const myByte = new BitByte(200);
console.log([...myByte]); // [ 1, 1, 0, 0, 1, 0, 0, 0 ]
const myByte = new BitByte(200);
// get
console.log(myByte[1], myByte[2]); // 1 0
// set
myByte[1] = 0;
myByte[2] = 1;
const myByte = new BitByte(200);
console.log(myByte == 200); // true
console.log(myByte + 0); // 200
console.log(new Number(myByte)); // [Number: 200]
const myByte = new BitByte(200);
console.log(new String(myByte)); // [String: '11001000']
console.log(myByte.toString()); // '11001000'
For those who want to use function calls.
Assigns array of bits to an instance.
Returns a bit from requested offset in range from 0 to 7.
Sets a bit on requested offset in range from 0 to 7.
bit
must be Number
(0
or 1
) or Boolean
.
Returns byte as an unsigned byte integer.
Returns character from a byte.