BitSet

This class implements a vector of bits that grows as needed. Each component of the bit set has a {@code bool} value. The bits of a {@code BitSet} are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One {@code BitSet} may be used to modify the contents of another {@code BitSet} through logical AND, logical inclusive OR, and logical exclusive OR operations.

<p>By default, all bits in the set initially have the value {@code false}.

<p>Every bit set has a current size, which is the number of bits of space currently in use by the bit set. Note that the size is related to the implementation of a bit set, so it may change with implementation. The length of a bit set relates to logical length of a bit set and is defined independently of implementation.

<p>Unless otherwise noted, passing a null parameter to any of the methods in a {@code BitSet} will result in a {@code NullPointerException}.

<p>A {@code BitSet} is not safe for multithreaded use without external synchronization.

@author Arthur van Hoff @author Michael McCloskey @author Martin Buchholz

Constructors

this
this()

Creates a new bit set. All bits are initially {@code false}.

this
this(size_t nbits)

Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range {@code 0} through {@code nbits-1}. All bits are initially {@code false}.

Members

Functions

and
void and(BitSet set)

Performs a logical <b>AND</b> of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value {@code true} if and only if it both initially had the value {@code true} and the corresponding bit in the bit set argument also had the value {@code true}.

andNot
void andNot(BitSet set)

Clears all of the bits in this {@code BitSet} whose corresponding bit is set in the specified {@code BitSet}.

cardinality
size_t cardinality()

Returns the number of bits set to {@code true} in this {@code BitSet}.

clear
void clear(size_t bitIndex)

Sets the bit specified by the index to {@code false}.

clear
void clear(size_t fromIndex, size_t toIndex)

Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to {@code false}.

clear
void clear()

Sets all of the bits in this BitSet to {@code false}.

equals
bool equals(Object obj)

Compares this object against the specified object. The result is {@code true} if and only if the argument is not {@code null} and is a {@code Bitset} object that has exactly the same set of bits set to {@code true} as this bit set. That is, for every nonnegative {@code int} index {@code k}, <pre>((BitSet)obj).get(k) == this.get(k)</pre> must be true. The current sizes of the two bit sets are not compared.

flip
void flip(size_t bitIndex)

Sets the bit at the specified index to the complement of its current value.

get
bool get(size_t bitIndex)

Returns the value of the bit with the specified index. The value is {@code true} if the bit with the index {@code bitIndex} is currently set in this {@code BitSet}; otherwise, the result is {@code false}.

get
BitSet get(size_t fromIndex, size_t toIndex)

Returns a new {@code BitSet} composed of bits from this {@code BitSet} from {@code fromIndex} (inclusive) to {@code toIndex} (exclusive).

intersects
bool intersects(BitSet set)

Returns true if the specified {@code BitSet} has any bits set to {@code true} that are also set to {@code true} in this {@code BitSet}.

isEmpty
bool isEmpty()

Returns true if this {@code BitSet} contains no bits that are set to {@code true}.

length
size_t length()

Returns the "logical size" of this {@code BitSet}: the index of the highest set bit in the {@code BitSet} plus one. Returns zero if the {@code BitSet} contains no set bits.

nextClearBit
size_t nextClearBit(int fromIndex)

Returns the index of the first bit that is set to {@code false} that occurs on or after the specified starting index.

nextSetBit
size_t nextSetBit(size_t fromIndex)

Returns the index of the first bit that is set to {@code true} that occurs on or after the specified starting index. If no such bit exists then {@code -1} is returned.

or
void or(BitSet set)

Performs a logical <b>OR</b> of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value {@code true} if and only if it either already had the value {@code true} or the corresponding bit in the bit set argument has the value {@code true}.

previousClearBit
size_t previousClearBit(size_t fromIndex)

Returns the index of the nearest bit that is set to {@code false} that occurs on or before the specified starting index. If no such bit exists, or if {@code -1} is given as the starting index, then {@code -1} is returned.

previousSetBit
size_t previousSetBit(size_t fromIndex)

Returns the index of the nearest bit that is set to {@code true} that occurs on or before the specified starting index. If no such bit exists, or if {@code -1} is given as the starting index, then {@code -1} is returned.

set
void set(size_t bitIndex)

Sets the bit at the specified index to {@code true}.

set
void set(int bitIndex, bool value)

Sets the bit at the specified index to the specified value.

set
void set(int fromIndex, int toIndex)

Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to {@code true}.

set
void set(int fromIndex, int toIndex, bool value)

Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to the specified value.

size
size_t size()

Returns the number of bits of space actually in use by this {@code BitSet} to represent bit values. The maximum element in the set is the size - 1st element.

toHash
size_t toHash()

Returns the hash code value for this bit set. The hash code depends only on which bits are set within this {@code BitSet}.

toLongArray
long[] toLongArray()

* Returns a new long array containing all the bits in this bit set. * * <p>More precisely, if * <br>{@code long[] longs = s.toLongArray();} * <br>then {@code longs.length == (s.length()+63)/64} and * <br>{@code s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)} * <br>for all {@code n < 64 * longs.length}. * * @return a long array containing a little-endian representation * of all the bits in this bit set

xor
void xor(BitSet set)

Performs a logical <b>XOR</b> of this bit set with the bit set argument. This bit set is modified so that a bit in it has the value {@code true} if and only if one of the following statements holds: <ul> <li>The bit initially has the value {@code true}, and the corresponding bit in the argument has the value {@code false}. <li>The bit initially has the value {@code false}, and the corresponding bit in the argument has the value {@code true}. </ul>

Meta