java.util

Class BitSet

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.
See Also:
Serialized Form

Constructor Summary

BitSet()
Create a new empty bit set.
BitSet(int nbits)
Create a new empty bit set, with a given size.

Method Summary

void
and(BitSet bs)
Performs the logical AND operation on this bit set and the given set.
void
andNot(BitSet bs)
Performs the logical AND operation on this bit set and the complement of the given bs.
int
cardinality()
Returns the number of bits set to true.
void
clear()
Sets all bits in the set to false.
void
clear(int pos)
Removes the integer pos from this set.
void
clear(int from, int to)
Sets the bits between from (inclusive) and to (exclusive) to false.
Object
clone()
Create a clone of this bit set, that is an instance of the same class and contains the same elements.
boolean
equals(Object obj)
Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.
void
flip(int index)
Sets the bit at the index to the opposite value.
void
flip(int from, int to)
Sets a range of bits to the opposite value.
boolean
get(int pos)
Returns true if the integer bitIndex is in this bit set, otherwise false.
BitSet
get(int from, int to)
Returns a new BitSet composed of a range of bits from this one.
int
hashCode()
Returns a hash code value for this bit set.
boolean
intersects(BitSet set)
Returns true if the specified BitSet and this one share at least one common true bit.
boolean
isEmpty()
Returns true if this set contains no true bits.
int
length()
Returns the logical number of bits actually used by this bit set.
int
nextClearBit(int from)
Returns the index of the next false bit, from the specified bit (inclusive).
int
nextSetBit(int from)
Returns the index of the next true bit, from the specified bit (inclusive).
void
or(BitSet bs)
Performs the logical OR operation on this bit set and the given set.
void
set(int pos)
Add the integer bitIndex to this set.
void
set(int index, boolean value)
Sets the bit at the given index to the specified value.
void
set(int from, int to)
Sets the bits between from (inclusive) and to (exclusive) to true.
void
set(int from, int to, boolean value)
Sets the bits between from (inclusive) and to (exclusive) to the specified value.
int
size()
Returns the number of bits actually used by this bit set.
String
toString()
Returns the string representation of this bit set.
void
xor(BitSet bs)
Performs the logical XOR operation on this bit set and the given set.

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

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:
nbits - the initial size of the bit set
Throws:
NegativeArraySizeException - if nbits < 0

Method Details

and

public void and(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:
bs - the second bit set
Throws:
NullPointerException - if bs is null

andNot

public void andNot(BitSet bs)
Performs the logical AND operation on this bit set and the complement of the given bs. 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 and is effectively the set difference of the two.
Parameters:
bs - the second bit set
Throws:
NullPointerException - if bs is null
Since:
1.2

cardinality

public int cardinality()
Returns the number of bits set to true.
Returns:
the number of true bits
Since:
1.4

clear

public void clear()
Sets all bits in the set to false.
Since:
1.4

clear

public void clear(int pos)
Removes the integer pos from this set. That is the corresponding bit is cleared. If the index is not in the set, this method does nothing.
Parameters:
pos - a non-negative integer
Throws:
IndexOutOfBoundsException - if pos < 0

clear

public void clear(int from,
                  int to)
Sets the bits between from (inclusive) and to (exclusive) to false.
Parameters:
from - the start range (inclusive)
to - the end range (exclusive)
Throws:
IndexOutOfBoundsException - if from < 0 || to < 0 || from > to
Since:
1.4

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.
Overrides:
clone in interface Object
Returns:
the clone of this object.

equals

public boolean equals(Object obj)
Returns true if the obj is a bit set that contains exactly the same elements as this bit set, otherwise false.
Overrides:
equals in interface Object
Parameters:
obj - the object to compare to
Returns:
true if obj equals this bit set

flip

public void flip(int index)
Sets the bit at the index to the opposite value.
Parameters:
index - the index of the bit
Throws:
IndexOutOfBoundsException - if index is negative
Since:
1.4

flip

public void flip(int from,
                 int to)
Sets a range of bits to the opposite value.
Parameters:
from - the low index (inclusive)
to - the high index (exclusive)
Throws:
IndexOutOfBoundsException - if from > to || from < 0 || to < 0
Since:
1.4

get

public boolean get(int pos)
Returns true if the integer bitIndex is in this bit set, otherwise false.
Parameters:
pos - a non-negative integer
Returns:
the value of the bit at the specified position
Throws:
IndexOutOfBoundsException - if the pos is negative

get

public BitSet get(int from,
                  int to)
Returns a new BitSet composed of a range of bits from this one.
Parameters:
from - the low index (inclusive)
to - the high index (exclusive)
Throws:
IndexOutOfBoundsException - if from > to || from < 0 || to < 0
Since:
1.4

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.
Overrides:
hashCode in interface Object
Returns:
the hash code value for this bit set.

intersects

public boolean intersects(BitSet set)
Returns true if the specified BitSet and this one share at least one common true bit.
Parameters:
set - the set to check for intersection
Returns:
true if the sets intersect
Throws:
NullPointerException - if set is null
Since:
1.4

isEmpty

public boolean isEmpty()
Returns true if this set contains no true bits.
Returns:
true if all bits are false
Since:
1.4

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:
the index of the highest set bit plus one.

nextClearBit

public int nextClearBit(int from)
Returns the index of the next false bit, from the specified bit (inclusive).
Parameters:
from - the start location
Returns:
the first false bit
Throws:
IndexOutOfBoundsException - if from is negative
Since:
1.4

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
}
Parameters:
from - the start location
Returns:
the first true bit, or -1
Throws:
IndexOutOfBoundsException - if from is negative
Since:
1.4

or

public void or(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:
bs - the second bit set
Throws:
NullPointerException - if bs is null

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:
pos - a non-negative integer.
Throws:
IndexOutOfBoundsException - if pos is negative

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.
Parameters:
index - the position to set
value - the value to set it to
Throws:
IndexOutOfBoundsException - if index is negative
Since:
1.4

set

public void set(int from,
                int to)
Sets the bits between from (inclusive) and to (exclusive) to true.
Parameters:
from - the start range (inclusive)
to - the end range (exclusive)
Throws:
IndexOutOfBoundsException - if from < 0 || from > to || to < 0
Since:
1.4

set

public void set(int from,
                int to,
                boolean value)
Sets the bits between from (inclusive) and to (exclusive) to the specified value.
Parameters:
from - the start range (inclusive)
to - the end range (exclusive)
value - the value to set it to
Throws:
IndexOutOfBoundsException - if from < 0 || from > to || to < 0
Since:
1.4

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:
the number of bits currently used.

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}".
Overrides:
toString in interface Object
Returns:
the string representation.

xor

public void xor(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:
bs - the second bit set
Throws:
NullPointerException - if bs is null

BitSet.java -- A vector of bits. Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.