Home » openjdk-7 » java » util » [javadoc | source]
java.util
public class: BitSet [javadoc | source]
java.lang.Object
   java.util.BitSet

All Implemented Interfaces:
    Cloneable, java$io$Serializable

This class implements a vector of bits that grows as needed. Each component of the bit set has a {@code boolean} 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.

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

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.

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

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

Constructor:
 public BitSet() 
 public BitSet(int nbits) 
Method from java.util.BitSet Summary:
and,   andNot,   cardinality,   clear,   clear,   clear,   clone,   equals,   flip,   flip,   get,   get,   hashCode,   intersects,   isEmpty,   length,   nextClearBit,   nextSetBit,   or,   previousClearBit,   previousSetBit,   set,   set,   set,   set,   size,   toByteArray,   toLongArray,   toString,   valueOf,   valueOf,   valueOf,   valueOf,   xor
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.util.BitSet Detail:
 public  void and(BitSet set) 
    Performs a logical AND 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}.
 public  void andNot(BitSet set) 
    Clears all of the bits in this {@code BitSet} whose corresponding bit is set in the specified {@code BitSet}.
 public int cardinality() 
    Returns the number of bits set to {@code true} in this {@code BitSet}.
 public  void clear() 
    Sets all of the bits in this BitSet to {@code false}.
 public  void clear(int bitIndex) 
    Sets the bit specified by the index to {@code false}.
 public  void clear(int fromIndex,
    int toIndex) 
    Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to {@code false}.
 public Object clone() 
    Cloning this {@code BitSet} produces a new {@code BitSet} that is equal to it. The clone of the bit set is another bit set that has exactly the same bits set to {@code true} as this bit set.
 public boolean 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},
    ((BitSet)obj).get(k) == this.get(k)
    must be true. The current sizes of the two bit sets are not compared.
 public  void flip(int bitIndex) 
    Sets the bit at the specified index to the complement of its current value.
 public  void flip(int fromIndex,
    int toIndex) 
    Sets each bit from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to the complement of its current value.
 public boolean get(int 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}.
 public BitSet get(int fromIndex,
    int toIndex) 
    Returns a new {@code BitSet} composed of bits from this {@code BitSet} from {@code fromIndex} (inclusive) to {@code toIndex} (exclusive).
 public int hashCode() 
    Returns the hash code value for this bit set. The hash code depends only on which bits are set within this {@code BitSet}.

    The hash code is defined to be the result of the following calculation:

     {@code
    public int hashCode() {
        long h = 1234;
        long[] words = toLongArray();
        for (int i = words.length; --i >= 0; )
            h ^= words[i] * (i + 1);
        return (int)((h >> 32) ^ h);
    }}
    Note that the hash code changes if the set of bits is altered.
 public boolean 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}.
 public boolean isEmpty() 
    Returns true if this {@code BitSet} contains no bits that are set to {@code true}.
 public int 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.
 public int 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.
 public int nextSetBit(int 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.

    To iterate over the {@code true} bits in a {@code BitSet}, use the following loop:

     {@code
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
        // operate on index i here
    }}
 public  void or(BitSet set) 
    Performs a logical OR 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}.
 public int previousClearBit(int 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.
 public int previousSetBit(int 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.

    To iterate over the {@code true} bits in a {@code BitSet}, use the following loop:

     {@code
    for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
        // operate on index i here
    }}
 public  void set(int bitIndex) 
    Sets the bit at the specified index to {@code true}.
 public  void set(int bitIndex,
    boolean value) 
    Sets the bit at the specified index to the specified value.
 public  void set(int fromIndex,
    int toIndex) 
    Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to {@code true}.
 public  void set(int fromIndex,
    int toIndex,
    boolean value) 
    Sets the bits from the specified {@code fromIndex} (inclusive) to the specified {@code toIndex} (exclusive) to the specified value.
 public int 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.
 public byte[] toByteArray() 
    Returns a new byte array containing all the bits in this bit set.

    More precisely, if
    {@code byte[] bytes = s.toByteArray();}
    then {@code bytes.length == (s.length()+7)/8} and
    {@code s.get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)}
    for all {@code n < 8 * bytes.length}.

 public long[] toLongArray() 
    Returns a new long array containing all the bits in this bit set.

    More precisely, if
    {@code long[] longs = s.toLongArray();}
    then {@code longs.length == (s.length()+63)/64} and
    {@code s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)}
    for all {@code n < 64 * longs.length}.

 public String toString() 
    Returns a string representation of this bit set. For every index for which this {@code BitSet} contains a bit in the set state, the decimal representation of that index is included in the result. Such indices are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces, resulting in the usual mathematical notation for a set of integers.

    Example:

    BitSet drPepper = new BitSet();
    Now {@code drPepper.toString()} returns "{@code {}}".

    drPepper.set(2);
    Now {@code drPepper.toString()} returns "{@code {2}}".

    drPepper.set(4);
    drPepper.set(10);
    Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
 public static BitSet valueOf(long[] longs) 
    Returns a new bit set containing all the bits in the given long array.

    More precisely,
    {@code BitSet.valueOf(longs).get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)}
    for all {@code n < 64 * longs.length}.

    This method is equivalent to {@code BitSet.valueOf(LongBuffer.wrap(longs))}.

 public static BitSet valueOf(LongBuffer lb) 
    Returns a new bit set containing all the bits in the given long buffer between its position and limit.

    More precisely,
    {@code BitSet.valueOf(lb).get(n) == ((lb.get(lb.position()+n/64) & (1L<<(n%64))) != 0)}
    for all {@code n < 64 * lb.remaining()}.

    The long buffer is not modified by this method, and no reference to the buffer is retained by the bit set.

 public static BitSet valueOf(byte[] bytes) 
    Returns a new bit set containing all the bits in the given byte array.

    More precisely,
    {@code BitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)}
    for all {@code n < 8 * bytes.length}.

    This method is equivalent to {@code BitSet.valueOf(ByteBuffer.wrap(bytes))}.

 public static BitSet valueOf(ByteBuffer bb) 
    Returns a new bit set containing all the bits in the given byte buffer between its position and limit.

    More precisely,
    {@code BitSet.valueOf(bb).get(n) == ((bb.get(bb.position()+n/8) & (1<<(n%8))) != 0)}
    for all {@code n < 8 * bb.remaining()}.

    The byte buffer is not modified by this method, and no reference to the buffer is retained by the bit set.

 public  void xor(BitSet set) 
    Performs a logical XOR 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:
    • The bit initially has the value {@code true}, and the corresponding bit in the argument has the value {@code false}.
    • The bit initially has the value {@code false}, and the corresponding bit in the argument has the value {@code true}.