- getting, setting and toggling of individual bits
- iterating over each bit
- counting the number of "on" bits
- bitwise operations with other bit arrays such as OR, AND and XOR.
- serialization to and from JSON
- Browser, Node.js and Ender.js compatible
The bit array is sparse. The following example shows how to set and get individual bits within the array:
a = new BitArray(32); a.set(0, true); a.set(31, true); a.toString(); // "10000000000000000000000000000001" a.get(1); // false a.get(31); // true
a.set(32, true); // throws an index of range exception
The BitArray module has two constructors:
- Creates a new empty bit array with the given size in bits.
- BitArray(size, hex)
- Creates a new bit array with the given size and using the hex string as value
The following instance methods are supported:
- Returns the total number of bits in the BitArray.
- set(index, boolean)
- Sets the bit at index to a value (boolean.)
- Returns the value of the bit at index (boolean.)
- Toggles the bit at index. If the bit is on, it is turned off. Likewise, if the bit is off it is turned on.
- Resets the BitArray so that it is empty and can be re-used.
- Returns a copy of this BitArray.
- Returns true if this BitArray equals another. Two BitArrays are considered equal if both have the same length and bit pattern.
- Returns the JSON representation of this BitArray.
- Returns a string representation of the BitArray with bits in logical order.
- Returns a hex representation of the BitArray.
- Convert the BitArray to an Array of boolean values.
- Returns the total number of bits set to 1 in this BitArray.
- Inverts this BitArray.
- Bitwise OR on the values of this BitArray using BitArray `other`.
- Bitwise AND on the values of this BitArray using BitArray `other`.
- Bitwise XOR on the values of this BitArray using BitArray `other`.
You can install the bit array module using npm:
> npm install bit-array
Alternatively you could just include bit-array.js in your project.
Licensed under the revised BSD License. Copyright 2010-2012 Bram Stein. All rights reserved.
https://github.com/foglcz/bit-array - PHP port by Pavel Ptacek