Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.util

Class BitSet

java.lang.Object
|
+--java.util.BitSet

All Implemented Interfaces:

Cloneable, Serializable


public class BitSet

extends Object

implements Cloneable, Serializable

This class can be thought of in two ways. You can see it as a vector of bits or as a set of non-negative integers. The name BitSet is a bit misleading. It is implemented by a bit vector, but its equally possible to see it as set of non-negative integer; each integer in the set is represented by a set bit at the corresponding index. The size of this structure is determined by the highest integer in the set. You can union, intersect and build (symmetric) remainders, by invoking the logical operations and, or, andNot, resp. xor. This implementation is NOT synchronized against concurrent access from multiple threads. Specifically, if one thread is reading from a bitset while another thread is simultaneously modifying it, the results are undefined.

Authors:

Constructor Summary

BitSet()

Create a new empty bit set.
BitSet(int nbits)

Create a new empty bit set, with a given size.

Method Summary

voidand(java.util.BitSet bs)

Performs the logical AND operation on this bit set and the given set.
voidandNot(java.util.BitSet bs)

Performs the logical AND operation on this bit set and the complement of the given set.
intcardinality()

Returns the number of bits set to true.
voidclear()

Sets all bits in the set to false.
voidclear(int pos)

Removes the integer bitIndex from this set.
voidclear(int from, int to)

Sets the bits between from (inclusive) and to (exclusive) to false.
java.lang.Objectclone()

Create a clone of this bit set, that is an instance of the same class and contains the same elements.
booleanequals(java.lang.Object obj)

Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.
voidflip(int index)

Sets the bit at the index to the opposite value.
voidflip(int from, int to)

Sets a range of bits to the opposite value.
booleanget(int pos)

Returns true if the integer bitIndex is in this bit set, otherwise false.
java.util.BitSetget(int from, int to)

Returns a new BitSet composed of a range of bits from this one.
inthashCode()

Returns a hash code value for this bit set.
booleanintersects(java.util.BitSet set)

Returns true if the specified BitSet and this one share at least one common true bit.
booleanisEmpty()

Returns true if this set contains no true bits.
intlength()

Returns the logical number of bits actually used by this bit set.
intnextClearBit(int from)

Returns the index of the next false bit, from the specified bit (inclusive).
intnextSetBit(int from)

Returns the index of the next true bit, from the specified bit (inclusive).
voidor(java.util.BitSet bs)

Performs the logical OR operation on this bit set and the given set.
voidset(int pos)

Add the integer bitIndex to this set.
voidset(int index, boolean value)

Sets the bit at the given index to the specified value.
voidset(int from, int to)

Sets the bits between from (inclusive) and to (exclusive) to true.
voidset(int from, int to, boolean value)

Sets the bits between from (inclusive) and to (exclusive) to the specified value.
intsize()

Returns the number of bits actually used by this bit set.
java.lang.StringtoString()

Returns the string representation of this bit set.
voidxor(java.util.BitSet bs)

Performs the logical XOR operation on this bit set and the given set.

Constructor Details

BitSet

public BitSet()

Create a new empty bit set. All bits are initially false.


BitSet

public BitSet(int nbits)

Create a new empty bit set, with a given size. This constructor reserves enough space to represent the integers from 0 to nbits-1.

Parameters:

Throws:


Method Details

and

public void and(java.util.BitSet bs)

Performs the logical AND operation on this bit set and the given set. This means it builds the intersection of the two sets. The result is stored into this bit set.

Parameters:

Throws:


andNot

public void andNot(java.util.BitSet bs)

Performs the logical AND operation on this bit set and the complement of the given set. This means it selects every element in the first set, that isn't in the second set. The result is stored into this bit set.

Since:Parameters:

Throws:


cardinality

public int cardinality()

Returns the number of bits set to true.

Since:Returns:


clear

public void clear()

Sets all bits in the set to false.

Since:

clear

public void clear(int pos)

Removes the integer bitIndex from this set. That is the corresponding bit is cleared. If the index is not in the set, this method does nothing.

Parameters:

Throws:


clear

public void clear(int from, int to)

Sets the bits between from (inclusive) and to (exclusive) to false.

Since:Parameters:

Throws:


clone

public Object clone()

Create a clone of this bit set, that is an instance of the same class and contains the same elements. But it doesn't change when this bit set changes.

Returns:


equals

public boolean equals(java.lang.Object obj)

Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.

Parameters:

Returns:


flip

public void flip(int index)

Sets the bit at the index to the opposite value.

Since:Parameters:

Throws:


flip

public void flip(int from, int to)

Sets a range of bits to the opposite value.

Since:Parameters:

Throws:


get

public boolean get(int pos)

Returns true if the integer bitIndex is in this bit set, otherwise false.

Parameters:

Returns:

Throws:


get

public BitSet get(int from, int to)

Returns a new BitSet composed of a range of bits from this one.

Since:Parameters:

Throws:


hashCode

public int hashCode()

Returns a hash code value for this bit set. The hash code of two bit sets containing the same integers is identical. The algorithm used to compute it is as follows: Suppose the bits in the BitSet were to be stored in an array of long integers called bits, in such a manner that bit k is set in the BitSet (for non-negative values of k) if and only if ((k/64) < bits.length) && ((bits[k/64] & (1L << (bit % 64))) != 0) Then the following definition of the hashCode method would be a correct implementation of the actual algorithm:
public int hashCode()
{
long h = 1234;
for (int i = bits.length-1; i >= 0; i--)
{
h ^= bits[i] * (i + 1);
}
return (int)((h >> 32) ^ h);
}
Note that the hash code values changes, if the set is changed.

Returns:


intersects

public boolean intersects(java.util.BitSet set)

Returns true if the specified BitSet and this one share at least one common true bit.

Since:Parameters:

Returns:

Throws:


isEmpty

public boolean isEmpty()

Returns true if this set contains no true bits.

Since:Returns:


length

public int length()

Returns the logical number of bits actually used by this bit set. It returns the index of the highest set bit plus one. Note that this method doesn't return the number of set bits.

Returns:


nextClearBit

public int nextClearBit(int from)

Returns the index of the next false bit, from the specified bit (inclusive).

Since:Parameters:

Returns:

Throws:


nextSetBit

public int nextSetBit(int from)

Returns the index of the next true bit, from the specified bit (inclusive). If there is none, -1 is returned. You can iterate over all true bits with this loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1))
{
// operate on i here
}

Since:Parameters:

Returns:

Throws:


or

public void or(java.util.BitSet bs)

Performs the logical OR operation on this bit set and the given set. This means it builds the union of the two sets. The result is stored into this bit set, which grows as necessary.

Parameters:

Throws:


set

public void set(int pos)

Add the integer bitIndex to this set. That is the corresponding bit is set to true. If the index was already in the set, this method does nothing. The size of this structure is automatically increased as necessary.

Parameters:

Throws:


set

public void set(int index, boolean value)

Sets the bit at the given index to the specified value. The size of this structure is automatically increased as necessary.

Since:Parameters:

Throws:


set

public void set(int from, int to)

Sets the bits between from (inclusive) and to (exclusive) to true.

Since:Parameters:

Throws:


set

public void set(int from, int to, boolean value)

Sets the bits between from (inclusive) and to (exclusive) to the specified value.

Since:Parameters:

Throws:


size

public int size()

Returns the number of bits actually used by this bit set. Note that this method doesn't return the number of set bits, and that future requests for larger bits will make this automatically grow.

Returns:


toString

public String toString()

Returns the string representation of this bit set. This consists of a comma separated list of the integers in this set surrounded by curly braces. There is a space after each comma. A sample string is thus "{1, 3, 53}".

Returns:


xor

public void xor(java.util.BitSet bs)

Performs the logical XOR operation on this bit set and the given set. This means it builds the symmetric remainder of the two sets (the elements that are in one set, but not in the other). The result is stored into this bit set, which grows as necessary.

Parameters:

Throws: