public class

BitSet

extends Object
implements Serializable Cloneable
java.lang.Object
   ↳ java.util.BitSet

Class Overview

The BitSet class implements a bit field. Each element in a BitSet can be on(1) or off(0). A BitSet is created with a given size and grows if this size is exceeded. Growth is always rounded to a 64 bit boundary.

Summary

Public Constructors
BitSet()
Create a new BitSet with size equal to 64 bits.
BitSet(int nbits)
Create a new BitSet with size equal to nbits.
Public Methods
void and(BitSet bs)
Performs the logical AND of this BitSet with another BitSet.
void andNot(BitSet bs)
Clears all bits in the receiver which are also set in the parameter BitSet.
int cardinality()
Returns the number of bits that are true in this BitSet.
void clear(int index)
Clears the bit at index index.
void clear()
Clears all the bits in this BitSet.
void clear(int fromIndex, int toIndex)
Clears the bits starting from fromIndex to toIndex.
Object clone()
Creates a copy of this BitSet.
boolean equals(Object obj)
Compares the argument to this BitSet and returns whether they are equal.
void flip(int index)
Flips the bit at index index.
void flip(int fromIndex, int toIndex)
Flips the bits starting from fromIndex to toIndex.
boolean get(int index)
Retrieves the bit at index index.
BitSet get(int fromIndex, int toIndex)
Retrieves the bits starting from fromIndex to toIndex and returns back a new bitset made of these bits.
int hashCode()
Computes the hash code for this BitSet.
boolean intersects(BitSet bs)
Checks if these two BitSets have at least one bit set to true in the same position.
boolean isEmpty()
Returns true if all the bits in this BitSet are set to false.
int length()
Returns the number of bits up to and including the highest bit set.
int nextClearBit(int index)
Returns the position of the first bit that is false on or after index.
int nextSetBit(int index)
Returns the position of the first bit that is true on or after index.
void or(BitSet bs)
Performs the logical OR of this BitSet with another BitSet.
void set(int index, boolean val)
Sets the bit at index index to val.
void set(int fromIndex, int toIndex, boolean val)
Sets the bits starting from fromIndex to toIndex to the given val.
void set(int fromIndex, int toIndex)
Sets the bits starting from fromIndex to toIndex.
void set(int index)
Sets the bit at index index to 1.
int size()
Returns the number of bits this BitSet has.
String toString()
Returns a string containing a concise, human-readable description of the receiver.
void xor(BitSet bs)
Performs the logical XOR of this BitSet with another BitSet.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public BitSet ()

Since: API Level 1

public BitSet (int nbits)

Since: API Level 1

Create a new BitSet with size equal to nbits. If nbits is not a multiple of 64, then create a BitSet with size nbits rounded to the next closest multiple of 64.

Parameters
nbits the size of the bit set.
Throws
NegativeArraySizeException if nbits is negative.

Public Methods

public void and (BitSet bs)

Since: API Level 1

Performs the logical AND of this BitSet with another BitSet. The values of this BitSet are changed accordingly.

Parameters
bs BitSet to AND with.

public void andNot (BitSet bs)

Since: API Level 1

Clears all bits in the receiver which are also set in the parameter BitSet. The values of this BitSet are changed accordingly.

Parameters
bs BitSet to ANDNOT with.

public int cardinality ()

Since: API Level 1

Returns the number of bits that are true in this BitSet.

Returns
  • the number of true bits in the set.

public void clear (int index)

Since: API Level 1

Clears the bit at index index. Grows the BitSet if index > size.

Parameters
index the index of the bit to clear.
Throws
IndexOutOfBoundsException if index is negative.
See Also

public void clear ()

Since: API Level 1

Clears all the bits in this BitSet.

public void clear (int fromIndex, int toIndex)

Since: API Level 1

Clears the bits starting from fromIndex to toIndex. Grows the BitSet if toIndex > size.

Parameters
fromIndex inclusive beginning position.
toIndex exclusive ending position.
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.
See Also

public Object clone ()

Since: API Level 1

Creates a copy of this BitSet.

Returns
  • a copy of this BitSet.

public boolean equals (Object obj)

Since: API Level 1

Compares the argument to this BitSet and returns whether they are equal. The object must be an instance of BitSet with the same bits set.

Parameters
obj the BitSet object to compare.
Returns
  • a boolean indicating whether or not this BitSet and obj are equal.
See Also

public void flip (int index)

Since: API Level 1

Flips the bit at index index. Grows the BitSet if index > size.

Parameters
index the index of the bit to flip.
Throws
IndexOutOfBoundsException if index is negative.
See Also

public void flip (int fromIndex, int toIndex)

Since: API Level 1

Flips the bits starting from fromIndex to toIndex. Grows the BitSet if toIndex > size.

Parameters
fromIndex inclusive beginning position.
toIndex exclusive ending position.
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.
See Also

public boolean get (int index)

Since: API Level 1

Retrieves the bit at index index. Grows the BitSet if index > size.

Parameters
index the index of the bit to be retrieved.
Returns
  • true if the bit at index is set, false otherwise.
Throws
IndexOutOfBoundsException if index is negative.

public BitSet get (int fromIndex, int toIndex)

Since: API Level 1

Retrieves the bits starting from fromIndex to toIndex and returns back a new bitset made of these bits. Grows the BitSet if toIndex > size.

Parameters
fromIndex inclusive beginning position.
toIndex exclusive ending position.
Returns
  • new bitset of the range specified.
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.
See Also

public int hashCode ()

Since: API Level 1

Computes the hash code for this BitSet. If two BitSets are equal the have to return the same result for hashCode().

Returns
  • the int representing the hash code for this bit set.

public boolean intersects (BitSet bs)

Since: API Level 1

Checks if these two BitSets have at least one bit set to true in the same position.

Parameters
bs BitSet used to calculate the intersection.
Returns
  • true if bs intersects with this BitSet, false otherwise.

public boolean isEmpty ()

Since: API Level 1

Returns true if all the bits in this BitSet are set to false.

Returns
  • true if the BitSet is empty, false otherwise.

public int length ()

Since: API Level 1

Returns the number of bits up to and including the highest bit set.

Returns
  • the length of the BitSet.

public int nextClearBit (int index)

Since: API Level 1

Returns the position of the first bit that is false on or after index.

Parameters
index the starting position (inclusive).
Returns
  • the position of the next bit set to false, even if it is further than this BitSet's size.

public int nextSetBit (int index)

Since: API Level 1

Returns the position of the first bit that is true on or after index.

Parameters
index the starting position (inclusive).
Returns
  • -1 if there is no bits that are set to true on or after index.

public void or (BitSet bs)

Since: API Level 1

Performs the logical OR of this BitSet with another BitSet. The values of this BitSet are changed accordingly.

Parameters
bs BitSet to OR with.

public void set (int index, boolean val)

Since: API Level 1

Sets the bit at index index to val. Grows the BitSet if index > size.

Parameters
index the index of the bit to set.
val value to set the bit.
Throws
IndexOutOfBoundsException if index is negative.
See Also

public void set (int fromIndex, int toIndex, boolean val)

Since: API Level 1

Sets the bits starting from fromIndex to toIndex to the given val. Grows the BitSet if toIndex > size.

Parameters
fromIndex inclusive beginning position.
toIndex exclusive ending position.
val value to set these bits.
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.
See Also

public void set (int fromIndex, int toIndex)

Since: API Level 1

Sets the bits starting from fromIndex to toIndex. Grows the BitSet if toIndex > size.

Parameters
fromIndex inclusive beginning position.
toIndex exclusive ending position.
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.
See Also

public void set (int index)

Since: API Level 1

Sets the bit at index index to 1. Grows the BitSet if index > size.

Parameters
index the index of the bit to set.
Throws
IndexOutOfBoundsException if index is negative.

public int size ()

Since: API Level 1

Returns the number of bits this BitSet has.

Returns
  • the number of bits contained in this BitSet.
See Also

public String toString ()

Since: API Level 1

Returns a string containing a concise, human-readable description of the receiver.

Returns
  • a comma delimited list of the indices of all bits that are set.

public void xor (BitSet bs)

Since: API Level 1

Performs the logical XOR of this BitSet with another BitSet. The values of this BitSet are changed accordingly.

Parameters
bs BitSet to XOR with.