Android APIs
public class

BitSet

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

Class Overview

The BitSet class implements a bit array. Each element is either true or false. A BitSet is created with a given size and grows automatically if this size is exceeded.

Summary

Public Constructors
BitSet()
Creates a new BitSet with size equal to 64 bits.
BitSet(int bitCount)
Creates a new BitSet with size equal to bitCount, rounded up to a multiple of 64.
Public Methods
void and(BitSet bs)
Logically ands the bits of this BitSet with bs.
void andNot(BitSet bs)
Clears all bits in this BitSet which are also set in bs.
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 range of bits [fromIndex, toIndex).
Object clone()
Creates and returns a copy of this Object.
boolean equals(Object o)
Compares this instance with the specified object and indicates if they are equal.
void flip(int index)
Flips the bit at index index.
void flip(int fromIndex, int toIndex)
Flips the range of bits [fromIndex, toIndex).
boolean get(int index)
Returns the bit at index index.
BitSet get(int fromIndex, int toIndex)
Returns a new BitSet containing the range of bits [fromIndex, toIndex), shifted down so that the bit at fromIndex is at bit 0 in the new BitSet.
int hashCode()
Returns an integer hash code for this object.
boolean intersects(BitSet bs)
Returns true if this.and(bs) is non-empty, but may be faster than computing that.
boolean isEmpty()
Returns true if all the bits in this BitSet are set to false, false otherwise.
int length()
Returns the number of bits up to and including the highest bit set.
int nextClearBit(int index)
Returns the index of the first bit that is clear on or after index.
int nextSetBit(int index)
Returns the index of the first bit that is set on or after index, or -1 if no higher bits are set.
void or(BitSet bs)
Logically ors the bits of this BitSet with bs.
int previousClearBit(int index)
Returns the index of the first bit that is clear on or before index, or -1 if no lower bits are clear or index == -1.
int previousSetBit(int index)
Returns the index of the first bit that is set on or before index, or -1 if no lower bits are set or index == -1.
void set(int index, boolean state)
Sets the bit at index index to state.
void set(int fromIndex, int toIndex, boolean state)
Sets the range of bits [fromIndex, toIndex) to state.
void set(int fromIndex, int toIndex)
Sets the range of bits [fromIndex, toIndex).
void set(int index)
Sets the bit at index index to true.
int size()
Returns the capacity in bits of the array implementing this BitSet.
byte[] toByteArray()
Returns a new byte[] containing a little-endian representation the bits of this BitSet, suitable for passing to valueOf to reconstruct this BitSet.
long[] toLongArray()
Returns a new long[] containing a little-endian representation of the bits of this BitSet, suitable for passing to valueOf to reconstruct this BitSet.
String toString()
Returns a string containing a concise, human-readable description of the receiver: a comma-delimited list of the indexes of all set bits.
static BitSet valueOf(byte[] bytes)
Equivalent to BitSet.valueOf(ByteBuffer.wrap(bytes)).
static BitSet valueOf(ByteBuffer byteBuffer)
Returns a BitSet corresponding to byteBuffer, interpreted as a little-endian sequence of bits.
static BitSet valueOf(LongBuffer longBuffer)
Returns a BitSet corresponding to longBuffer, interpreted as a little-endian sequence of bits.
static BitSet valueOf(long[] longs)
Equivalent to BitSet.valueOf(LongBuffer.wrap(longs)), but likely to be faster.
void xor(BitSet bs)
Logically xors the bits of this BitSet with bs.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public BitSet ()

Added in API level 1

Creates a new BitSet with size equal to 64 bits.

public BitSet (int bitCount)

Added in API level 1

Creates a new BitSet with size equal to bitCount, rounded up to a multiple of 64.

Parameters
bitCount int
Throws
NegativeArraySizeException if bitCount < 0.

Public Methods

public void and (BitSet bs)

Added in API level 1

Logically ands the bits of this BitSet with bs.

Parameters
bs BitSet

public void andNot (BitSet bs)

Added in API level 1

Clears all bits in this BitSet which are also set in bs.

Parameters
bs BitSet

public int cardinality ()

Added in API level 1

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

Returns
int

public void clear (int index)

Added in API level 1

Clears the bit at index index.

Parameters
index int
Throws
IndexOutOfBoundsException if index < 0.

public void clear ()

Added in API level 1

Clears all the bits in this BitSet. This method does not change the capacity. Use clear if you want to reuse this BitSet with the same capacity, but create a new BitSet if you're trying to potentially reclaim memory.

public void clear (int fromIndex, int toIndex)

Added in API level 1

Clears the range of bits [fromIndex, toIndex).

Parameters
fromIndex int
toIndex int
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.

public Object clone ()

Added in API level 1

Creates and returns a copy of this Object. The default implementation returns a so-called "shallow" copy: It creates a new instance of the same class and then copies the field values (including object references) from this instance to the new instance. A "deep" copy, in contrast, would also recursively clone nested objects. A subclass that needs to implement this kind of cloning should call super.clone() to create the new instance and then create deep copies of the nested, mutable objects.

Returns
Object a copy of this object.

public boolean equals (Object o)

Added in API level 1

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

Parameters
o Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

public void flip (int index)

Added in API level 1

Flips the bit at index index.

Parameters
index int
Throws
IndexOutOfBoundsException if index < 0.

public void flip (int fromIndex, int toIndex)

Added in API level 1

Flips the range of bits [fromIndex, toIndex).

Parameters
fromIndex int
toIndex int
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.

public boolean get (int index)

Added in API level 1

Returns the bit at index index. Indexes greater than the current length return false.

Parameters
index int
Returns
boolean
Throws
IndexOutOfBoundsException if index < 0.

public BitSet get (int fromIndex, int toIndex)

Added in API level 1

Returns a new BitSet containing the range of bits [fromIndex, toIndex), shifted down so that the bit at fromIndex is at bit 0 in the new BitSet.

Parameters
fromIndex int
toIndex int
Returns
BitSet
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.

public int hashCode ()

Added in API level 1

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

public boolean intersects (BitSet bs)

Added in API level 1

Returns true if this.and(bs) is non-empty, but may be faster than computing that.

Parameters
bs BitSet
Returns
boolean

public boolean isEmpty ()

Added in API level 1

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

Returns
boolean

public int length ()

Added in API level 1

Returns the number of bits up to and including the highest bit set. This is unrelated to the size() of the BitSet.

Returns
int

public int nextClearBit (int index)

Added in API level 1

Returns the index of the first bit that is clear on or after index. Since all bits past the end are implicitly clear, this never returns -1.

Parameters
index int
Returns
int
Throws
IndexOutOfBoundsException if index < 0.

public int nextSetBit (int index)

Added in API level 1

Returns the index of the first bit that is set on or after index, or -1 if no higher bits are set.

Parameters
index int
Returns
int
Throws
IndexOutOfBoundsException if index < 0.

public void or (BitSet bs)

Added in API level 1

Logically ors the bits of this BitSet with bs.

Parameters
bs BitSet

public int previousClearBit (int index)

Added in API level 19

Returns the index of the first bit that is clear on or before index, or -1 if no lower bits are clear or index == -1.

Parameters
index int
Returns
int
Throws
IndexOutOfBoundsException if index < -1.

public int previousSetBit (int index)

Added in API level 19

Returns the index of the first bit that is set on or before index, or -1 if no lower bits are set or index == -1.

Parameters
index int
Returns
int
Throws
IndexOutOfBoundsException if index < -1.

public void set (int index, boolean state)

Added in API level 1

Sets the bit at index index to state.

Parameters
index int
state boolean
Throws
IndexOutOfBoundsException if index < 0.

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

Added in API level 1

Sets the range of bits [fromIndex, toIndex) to state.

Parameters
fromIndex int
toIndex int
state boolean
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.

public void set (int fromIndex, int toIndex)

Added in API level 1

Sets the range of bits [fromIndex, toIndex).

Parameters
fromIndex int
toIndex int
Throws
IndexOutOfBoundsException if fromIndex or toIndex is negative, or if toIndex is smaller than fromIndex.

public void set (int index)

Added in API level 1

Sets the bit at index index to true.

Parameters
index int
Throws
IndexOutOfBoundsException if index < 0.

public int size ()

Added in API level 1

Returns the capacity in bits of the array implementing this BitSet. This is unrelated to the length of the BitSet, and not generally useful. Use nextSetBit(int) to iterate, or length() to find the highest set bit.

Returns
int

public byte[] toByteArray ()

Added in API level 19

Returns a new byte[] containing a little-endian representation the bits of this BitSet, suitable for passing to valueOf to reconstruct this BitSet.

Returns
byte[]

public long[] toLongArray ()

Added in API level 19

Returns a new long[] containing a little-endian representation of the bits of this BitSet, suitable for passing to valueOf to reconstruct this BitSet.

Returns
long[]

public String toString ()

Added in API level 1

Returns a string containing a concise, human-readable description of the receiver: a comma-delimited list of the indexes of all set bits. For example: "{0,1,8"}.

Returns
String a printable representation of this object.

public static BitSet valueOf (byte[] bytes)

Added in API level 19

Equivalent to BitSet.valueOf(ByteBuffer.wrap(bytes)).

Parameters
bytes byte
Returns
BitSet

public static BitSet valueOf (ByteBuffer byteBuffer)

Added in API level 19

Returns a BitSet corresponding to byteBuffer, interpreted as a little-endian sequence of bits. This method does not alter the ByteBuffer.

Parameters
byteBuffer ByteBuffer
Returns
BitSet

public static BitSet valueOf (LongBuffer longBuffer)

Added in API level 19

Returns a BitSet corresponding to longBuffer, interpreted as a little-endian sequence of bits. This method does not alter the LongBuffer.

Parameters
longBuffer LongBuffer
Returns
BitSet

public static BitSet valueOf (long[] longs)

Added in API level 19

Equivalent to BitSet.valueOf(LongBuffer.wrap(longs)), but likely to be faster. This is likely to be the fastest way to create a BitSet because it's closest to the internal representation.

Parameters
longs long
Returns
BitSet

public void xor (BitSet bs)

Added in API level 1

Logically xors the bits of this BitSet with bs.

Parameters
bs BitSet