A data structure that holds a sequence of bits and contains convenient methods for common bitwise operations.
Members
(readonly) length :Number
The index of the highest set bit plus one or the minimum set length, whichever is higher.
Type:
- Number
Methods
clone() → {BitSet}
Produces a new bitset instance equal to this.
Returns:
- Type
- BitSet
copy(bitset) → {this}
Copies a bitsetlike value or instance. Minimum length is preserved.
Parameters:
Name | Type | Description |
---|---|---|
bitset |
BitSetLike |
Returns:
This instance.
- Type
- this
count() → {Number}
Gets the number of set bits (1's) in this bitset.
For the total number of bits, see BitSet#length.
Returns:
- Type
- Number
equals(other) → {Boolean}
Checks whether this bitset is equal to another bitsetlike value or instance. They are equal if
if their set bits match up.
Parameters:
Name | Type | Description |
---|---|---|
other |
BitSetLike |
Returns:
- Type
- Boolean
flip(…masks) → {this}
Flips bits based on one or more masks. Equivalent to a bitwise XOR operation.
Only set bits in any of the provided bitmasks are affected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
This instance.
- Type
- this
flipAll() → {this}
Flips all bits in this bitset.
Returns:
This instance.
- Type
- this
flipAt(index) → {this}
Flips a bit at a specific index.
Parameters:
Name | Type | Description |
---|---|---|
index |
Number | Starting at 0, from the rightmost bit. |
Throws:
-
In case 'index' is not an integer or out of bounds.
- Type
- Error
Returns:
This instance.
- Type
- this
flipRange(from, to) → {this}
Flips all bits within a specific range.
Parameters:
Name | Type | Description |
---|---|---|
from |
Number | Starting index, from the rightmost bit. |
to |
Number | The exclusive ending index. |
Returns:
This instance.
- Type
- this
get(index) → {Boolean}
Gets the bit at a specific index.
Example: 001 at index 0 yields 1.
Parameters:
Name | Type | Description |
---|---|---|
index |
Number |
Returns:
- Type
- Boolean
getRange(from, to) → {BitSet}
Gets the bits within a specific index range.
Example: 011 from 0 to 2 yields 11.
Parameters:
Name | Type | Description |
---|---|---|
from |
Number | The inclusive lower bounds of the range. |
to |
Number | The exclusive upper bounds of the range. |
Returns:
The Resulting BitSet.
- Type
- BitSet
intersect(…masks) → {this}
Intersects this bitset with one or more bitmasks, only affecting this instance.
Equivalent to a bitwise AND operation.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
This instance.
- Type
- this
intersects(…masks) → {Boolean}
Checks whether this bitset intersects with one or more bitmasks. They intersect if any set bits in this bitset
are also set in any of the provided bitmasks.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
- Type
- Boolean
off(…masks) → {this}
Sets bits to 0 based on one or more masks. Equivalent to a bitwise AND NOT operation.
Only set bits in any of the provided bitmasks are affected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
This instance.
- Type
- this
on(…masks) → {this}
Sets bits to 1 based on one or more masks. Equivalent to a bitwise OR operation.
Only set bits in any of the provided bitmasks are affected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
This instance.
- Type
- this
serialize() → {String}
Serializes this bitset.
Returns:
- Type
- String
set(value, …masks) → {this}
Sets bits to a specific value based on one or more masks.
Only set bits in any of the provided bitmasks are affected.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
value |
Bit | ||
masks |
BitMask |
<repeatable> |
Returns:
This instance.
- Type
- this
setAll(value) → {this}
Sets all bits in this bitset to a specific value.
Parameters:
Name | Type | Description |
---|---|---|
value |
Bit |
Returns:
This instance.
- Type
- this
setAt(value, index) → {this}
Sets a bit at a specific index to a specific value.
Parameters:
Name | Type | Description |
---|---|---|
value |
Bit | |
index |
Number | Starting at 0, from the rightmost bit. |
Throws:
-
In case 'index' is not an integer or out of bounds.
- Type
- Error
Returns:
This instance.
- Type
- this
setRange(value, from, to) → {this}
Sets all bits within a specific range to a specific value.
Parameters:
Name | Type | Description |
---|---|---|
value |
Bit | value |
from |
Number | Starting index, from the rightmost bit. |
to |
Number | The exclusive ending index. |
Returns:
This instance.
- Type
- this
test(…masks) → {Boolean}
Tests whether all bits are set based on one or more bitmasks.
Only set bits in any of the provided bitmasks are tested.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
- Type
- Boolean
testAll(value) → {Boolean}
Tests whether all bits in this bitset has one specific value.
Parameters:
Name | Type | Description |
---|---|---|
value |
Bit |
Returns:
- Type
- Boolean
testAny(…masks) → {Boolean}
Tests whether any bits are set based on one or more bitmasks.
Only set bits in any of the provided bitmasks are tested.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
masks |
BitMask |
<repeatable> |
Returns:
- Type
- Boolean
testAt(value, index) → {Boolean}
Tests whether a bit at a specific index is equal to a specific value.
Parameters:
Name | Type | Description |
---|---|---|
value |
Bit | |
index |
Number | Starting at 0, from the rightmost bit. |
Throws:
-
In case 'index' is not an integer or out of bounds.
- Type
- Error
Returns:
- Type
- Boolean
toArray() → {Array.<Boolean>}
Gets an array containing all the bits in this bitset.
Returns:
- Type
- Array.<Boolean>
toString() → {String}
Gets a string representation of this bitset.
Returns:
- Type
- String
valueOf() → {Number}
Gets the integer value of this bitset.
Returns:
- Type
- Number