
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
@zekfad/bitbytearray
Advanced tools
Simple basic bits array for JavaScript.
⚠️ Starting from version 2 all strings are encoded and decoded as UTF-16 (2 bytes per character).
Install via npm:
npm install --save @zekfad/bitbytearray
Install via yarn:
yarn add @zekfad/bitbytearray
Simple array of bits:
const BitByteArray = require('./BitByteArray.js');
const myBitsArray = new BitByteArray(32);
console.log([ ...myBitsArray, ]);
/*
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
]
*/
myBitsArray.assign([
0, 1, 0, 0, 1, 0, 0, 0,
]);
myBitsArray.assign(
[
0, 1, 1, 0, 1, 0, 0, 1,
],
16,
);
console.log(myBitsArray.toString()); // Hi
Manipulate strings:
const BitByteArray = require('./BitByteArray.js');
const utf8string = 'UTF-8 Строка';
const myBitsArray = BitByteArray.from(utf8string);
console.log([ ...myBitsArray, ]);
/*
[
0, 1, 0, 1, 0, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0,
... 92 more items
]
*/
console.log(utf8string === myBitsArray.toString()); // true
console.log(myBitsArray.toString()); // UTF-8 Строка
// Change dash to space
myBitsArray.assign(
[
0, 0, 1, 0, 0, 0, 0, 0,
],
3 * 8 * 2 // Skip 3 UTF-16 characters (8 * 2 bits)
)
console.log(myBitsArray.toString()); // UTF 8 Строка
Array-like access by index:
const BitByteArray = require('./BitByteArray.js');
const myBitsArray = BitByteArray.from(2);
console.log([...myBitsArray, ]);
/*
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0
]
*/
myBitsArray[29] = 1;
console.log([ ...myBitsArray, ]);
/*
[
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 1, 0
]
*/
console.log(
parseInt(
[ ...myBitsArray, ]
.join(''),
2
)
); // 6
This module doesn't implement all methods of normal Array, you should use spread operator ([ ...myBitsArray, ]), if you need to work with bits in array-like way, and then use BitByteArray.from(BitByteArray.projectBitsArray(myNormalArray)).
BitByteArray classBitByteArray.safe(length)Get class representing an array of bits with index assign checks.
length (number) - Bits array length.Returns BitByteArray
BitByteArray.from(source)Create an array of bits from existing data.
source (number|number[]|boolean|boolean[]|string) - Source data.Returns BitByteArray - Created BitByteArray.
BitByteArray.projectBitsArray(source)Create an array of ones and zeros from existing data.
source (Array) - Source data.Returns Array - Normalized array, ready to use with .from().
new BitByteArray(length)Create an array of bits.
length (number) - Bits array length.BitByteArray instancelengthGet/set array of bits' length.
setLength(length)Change array of bits' length.
length (number) - Bits array length.Returns number - Bits array length.
setBit(offset, bit)Change bit on a given offset.
offset (number) - Bit offset.bit (number|boolean) - Bit.Returns number - Bit.
getBit(offset)Get bit on a given offset.
offset (number) - Bit offset.Returns number - Bit.
getBytes()Get local storage of bytes as an array of numbers.
Returns number[] - Bytes array.
toString(encoding)Get local storage of bytes as a string.
encoding (string) - Encoding.Returns string - String representation of bits array.
assign(bits, offset)Assign array of bits to an instance.
bits (number[]|boolean[]) - Bits array.offset (number) - Assign offset.Returns boolean - Returns true if no errors found.
fill(bit)Fill local storage of bits with a provided bit.
bit (number|boolean) - Bit.Returns BitByteArray - Returns instance if no errors found.
*[Symbol.iterator]()Generate bits sequence.
Yields number - Next number in the sequence.
[index]Access a bit by index in array-like way (in read-write mode).
Returns number - Bit.
checkOffset(offset)Check if given offset is acceptable.
number) - Bit offset.Returns boolean - Returns true if given argument is acceptable.
setBytes(length)Change array of bits' count of bytes.
length (number) - Count of bytes.Returns number - Count of bytes.
FAQs
Array of bits using BitByte.
We found that @zekfad/bitbytearray demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.