Functions for doing bit manipulation of typed arrays.
npm i bits-to-bytes
const bits = require('bits-to-bytes')
const buffer = Uint8Array.from([0b11001000])
bits.get(buffer, 3)
// true
buffer
may be any typed array with a number
element type, including Uint8Array
, Uint16Array
, and Uint32Array
.
Get the number of bytes required to contain size
bits.
Get the given bit, which will either be true
(set) or false
(unset).
Set the given bit to value
, which defaults to true
. Returns true
if the bit changed, otherwise false
.
Set the given bit range to value
, which defaults to true
. Returns true
if any of the bits changed, otherwise false
.
If you don't need the additional information about whether any of the bits changed, consider bits.fill()
as a more performant alternative.
Fill the given bit range with value
. start
defaults to 0
and end
defaults to the bit length of the array. Returns the modified array.
Toggle the given bit, returning its new value.
Unset the given bit. Returns true
if the bit was set, otherwise false
.
Unset the given bit range. Returns true
if any of the bits were set, otherwise false
.
Return the index of the first occurrence of value
, or -1
if not found. If position
is given, return the first index that is greater than or equal to position
.
Return the index of the last occurrence of value
, or -1
if not found. If position
is given, return the last index that is less than or equal to position
.
Count the number of bits set in the given bit range. start
defaults to 0
and end
defaults to the bit length of the array.
Create a buffer containing the given bits.
Create a buffer containing the given bits.
Create an iterator over bits.
Apache 2.0