bits-bytes
Pack and unpack unsigned integers of arbitrary bit length to and from byte arrays
npm install bits-bytes
core
getByteBit
Get a bit value from a byte - using numbers not in range 0-255 will produce unexpected results
getByteBit byte: number, bitOffset: number 1 | 0
const getByteBit = // 1console
setByteBit
Set a bit value on a byte and return the new byte - using numbers not in range 0-255 will produce unexpected results
setByteBit byte: number, bitOffset: number, bit: any number
const setByteBit = const byte = // 1000console
getBit
Get a bit within a byte array at the specified bit offset
getBit bytes: Uint8Array, bitOffset: number 1 | 0
const getBit = const bytes = 0 0b0001000 // 1console
setBit
Set a bit within a byte array at the specified bit offset - bit
can be any
value, the bit will be set to 1
if truthy and 0
if falsey
setBit bytes: Uint8Array, bitOffset: number, bit: any void
const setBit = const bytes = 0 0 // 1000console
getUint
Read a uint from the byte array of the specified bitLength from the specified
bit offset - if not specified, bitOffset
defaults to 0
getUint bytes: Uint8Array, bitLength: number, bitOffset = 0 number
const getUint = const bytes = 0b10110001 const first = const second = // 5console // 17console
setUint
Set a uint in the byte array of the specified bitLength and value at the
specified bit offset - if not specified, bitOffset
defaults to 0
valueStrategy
is an optional function that handles values that are out of
range for the bit length - by default it uses %
to wrap the number into the
required range - clampStrategy
is also available and clamps the value into
the range 0-n
where n
is the maximum value for that bit length
setUint bytes: Uint8Array, bitLength: number, uint: number, bitOffset? = 0, valueStrategy: ValueStrategy = modStrategy void
const setUint clampStrategy = const bytes = 2 // 1010000console // 1010000console /* 13 % 8 = 5, 0b101*/ // 1010000console /* clamps to range 0-7, so 13 becomes 7*/ // 1110000console
unpack
Unpacks a sequence of uints from the byte array, where the bit lengths are
specified in the bitLengths
array, starting at the specified bit offset.
If omitted, bitOffset
is 0
unpack bytes: Uint8Array, bitLengths: number, bitOffset = 0 number
const unpack = const bytes = 0b10110001 const values = // [ 5, 17 ]console
pack
Packs a sequence of uints into the byte array, where pairs
is an array of
[ length, value ]
tuples, starting at the specified bit offset and using
an optional function valueStrategy
to handle any values that fall outside of
the range for their bit length. If omitted, bitOffset
is 0 and the
valueStrategy
uses %
to wrap the number into the required range
pack bytes: Uint8Array, pairs: LengthValuePair, bitOffset = 0, valueStrategy: ValueStrategy = modStrategy void
const pack = const bytes = 2 const firstLength = 3const firstValue = 5 const secondLength = 5const secondValue = 17 const pairs = firstLength firstValue secondLength secondValue // 10110001console const secondBytePairs = secondLength secondValue firstLength firstValue // 10001101console
create
Creates a new Uint8Array and packs a sequence of uints into it, where pairs
is
an array of [ length, value ]
tuples, starting at the specified bit offset and
using an optional function valueStrategy
to handle any values that fall
outside of the range for their bit length. If omitted, bitOffset
is 0 and the
valueStrategy
uses %
to wrap the number into the required range
create pairs: LengthValuePair, bitOffset = 0, valueStrategy: ValueStrategy = modStrategy
const create = const firstLength = 3const firstValue = 5 const secondLength = 5const secondValue = 17 const pairs = firstLength firstValue secondLength secondValue const bytes = // 10110001console
utils
countBytes
Counts the smallest numbers of bytes required to fit a sequence of uints
countBytes bitLengths: number number
const countBytes = // 1console // 2console
countBits
Sums an array of bit lengths
countBits bitLengths: number number
const countBits = // 8console // 10console
maxValue
The maximum number of different uint values that can be stored in a given number
of bits - note that the highest allowed value is 1 less than this, as the range
is 0-n
, not 1-n
maxValue bitLength: number number
const maxValue = // 256console
valueToBitLength
Find the number of bits required to store a given uint value
valueToBitLength value: number number
const valueToBitLength = // 8console// 9console
bitLengthToBytes
Returns the number of bytes required to store the given number of bits
bitLengthToBytes bitLength: number number
const bitLengthToBytes = // 1console// 2console
modStrategy
Uses modulo to wrap the given value into the range specified by bitLength
modStrategy value: number, bitLength: number number
const modStrategy = // 255console// 0console// 1console
clampStrategy
Clamps the given value into the range 0-n
, where n
is maximum value allowed
for the specified bitLength
clampStrategy value: number, bitLength: number number
const clampStrategy = // 255console// 0console// 255console
License
MIT License
Copyright (c) 2019 Nik Coughlin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.