BitArray
Utility library for dealing with bit / byte arrays and converting between node Buffers and bitarrays.
Usage
var BitArray =
Methods
BitArray
See: BitArray.factory
for details on instantiation
BitArray.factory([bits], [length], [asOctet])
Return a new BitArray instance. Added for use in Array.map(), if a buffer is passed
in, the asOctet
will always be set to true
.
bits
- 32bit integer, or buffer/array of 32bit integers (optional)length
- zero fill the bitarray to the set lengthasOctet
- ensure resulting array length is a multiple of 8 (optional, defaultfalse
)
255 128 // [ '11111111', '00000001' ]
BitArray.parse([bits], [asOctet])
Convert a 32bit integer into a bit array, the asOctet
will always be set to true
if a Buffer is passed in.
bits
- 32bit integer, or buffer/array of 32bit integers (optional)asOctet
- ensure resulting array length is a multiple of 8 (optional, defaultfalse
)
BitArray // [1, 1, 1, 1, 1, 1, 1, 1]
BitArray.octet(array)
Zero fill an array until it represents an octet
array
- bit array
BitArray // [1, 0, 0, 0, 0, 0, 0, 0]
BitArray.equals(bitarray, bitarray)
Perform an equality check on two BitArray instances
bitarray
- bit array to comparebitarray
- bit array to compare
var a = '0101' b = '0101' c = '10' BitArray // trueBitArray // false
BitArray.fromBinary(string)
Create a new instance from a binary string
string
- binary string
var bits = BitArraybits // [1, 0, 1, 1, 0, 0, 1]
BitArray.fromOffsets(array)
Create a new instance from a offset positions of set bits
string
- binary string
var bits = BitArraybits // [1, 0, 1, 0, 1, 0, 0, 0, 0, 1]
BitArray.fromNumber(number)
Create a new instance from a base 10 number
number
- base 10 number
Aliases: [fromDecimal
]
var bits = BitArraybits // [1, 1, 1, 1]
BitArray.fromHexadecimal(string)
Create a new instance from a hexadecimal string, case insensitive.
string
- hexadecimal string
Aliases: [fromHex
]
var bits = BitArraybits // [0, 1, 0, 1, 1, 1, 1, 1]
BitArray.from32Integer(number)
Create a new instance from a 32bit integer
number
- 32bit integer
var bits = BitArraybits // [1, 0, 0, 1, 0, 0, 0, 0]
BitArray.fromBuffer(buffer)
Create a new instance from a node buffer instance
buffer
- node buffer instance
Aliases: [fromRedis
]
var buf = 128 144 255 bits = BitArray bits // [1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]
BitArray.and(array1, [array2], [...])
Perform a bitwise AND
operation on any number of bit arrays
...
- any number of bit arrays
Alias: [intersect
]
BitArray//[ 0, 0, 0, 0, 0, 0, 0, 1 ]
BitArray.toOffsets(array)
Find the offset indexes of all set bits
array
- bit array
BitArray // [2, 4]144 // [0, 3]
BitArray.toBuffer(bits)
BitArray.toNumber(bits)
Convert an array of bits to a base 10 number
Alias: [toDecimal
]
BitArray // 45
BitArray.toHexadecimal(bits)
Convert an array of bits to a hex string. Results are lowercased.
Alias: [toHex
]
BitArray // '2d'
BitArray.or(array1, [array2], [...])
Perform a bitwise OR
operation on any number of bit arrays
...
- any number of bit arrays
Alias: [union
]
BitArray//[ 1, 1, 0, 1, 1, 1, 0, 1 ]
BitArray.xor(array1, [array2], [...])
Perform a bitwise XOR
operation on any number of bit arrays
...
- any number of bit arrays
Alias: [difference
]
BitArray//[ 0, 1, 0, 1, 1, 1, 0, 1 ]
BitArray.not(array)
Perform a bitwise NOT
operation on a single array
array
- single bit array to flip
Alias: [reverse
]
BitArray//[ 0, 1, 1, 0, 1, 1, 1, 0 ]
BitArray.bitcount(32bit)
Find the cardinality of a bit array, 32bit integer, or buffer of 32bit ints
bits
- 32bit integer, buffer of 32bit integers, or bit array
Alias: [count
, cardinality
, population
]
BitArray // 2BitArray // 1BitArray // 9BitArray // 3
instance.set(index, value)
Set the value of the bit array at the given index / offset
index
- offset position to set valuevalue
- bit val (1 / 0)
// bits: [0, 0, 1]
instance.get(index)
Get the value of the bit array at the given index / offset
index
- offset position to get
144 // 1
instance.fill(offset)
Zero fill the current bits to the given offset
offset
- zero fill the current bits to the given offset
// bits: [0, 0, 0, 0, 0, 0]
instance.bitcount()
Get the bitcount of the current bits
Alias: [count
, cardinality
, population
]
255 // 8
instance.toString()
Get the binary representation of the current bits, can also be used in string coercion.
128 // '00000001'255 // '11111111'
instance.valueOf()
Get the base 10 number representing the current bits, can also be used in number coercion.
Alias: [toNumber
, toDecimal
]
144 // 91 + 255 // 256
instance.toJSON()
Get a copy of the current bit array
Alias: [toArray
, toBits
]
128 // [1,0,0,0,0,0,0,0]
instance.toBuffer()
Convert the current bit array to a node Buffer
128 255 // <Buffer 80 ff>
instance.toOffsets()
Convert the current bit array to an offset array
128 255 // [0, 8, 9, 10, 11, 12, 13, 14, 15]
instance.copy()
Create and return a copy of the current BitArray
Aliases: [clone
]
var bits = 255 bits2 = bits BitArray // truebits === bits2 // false
instance.reset()
Reset the current bits, if a length was supplied to the constructor it will be used.
Aliases: [clear
]
var bits = 144 16bits // '000000000001001'
instance.equals(bitarray)
Determine if the instance is equal to another instance
bitarray
- instance to compare
var a = 1 // [1] b = 2 // [1, 0] c = // [1] a // falsea // true
instance.length
Get the length of the current bit array
length // 0128 255length // 16
Install
With npm
npm install node-bitarray
License
(The MIT License)
Copyright (c) 2013 Beau Sorensen mail@beausorensen.com
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.