to top
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.
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.
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.
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 ()

Since: API Level 1

Creates a new BitSet with size equal to 64 bits.

public BitSet (int bitCount)

Since: API Level 1

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

Throws
NegativeArraySizeException if bitCount < 0.

Public Methods

public void and (BitSet bs)

Since: API Level 1

Logically ands the bits of this BitSet with bs.

public void andNot (BitSet bs)

Since: API Level 1

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

public int cardinality ()

Since: API Level 1

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

public void clear (int index)

Since: API Level 1

Clears the bit at index index.

Throws
IndexOutOfBoundsException if index < 0.

public void clear ()

Since: 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)

Since: API Level 1

Clears the range of bits [fromIndex, toIndex).

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

public Object clone ()

Since: 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
  • a copy of this object.

public boolean equals (Object o)

Since: 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 the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public void flip (int index)

Since: API Level 1

Flips the bit at index index.

Throws
IndexOutOfBoundsException if index < 0.

public void flip (int fromIndex, int toIndex)

Since: API Level 1

Flips the range of bits [fromIndex, toIndex).

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

public boolean get (int index)

Since: API Level 1

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

Throws
IndexOutOfBoundsException if index < 0.

public BitSet get (int fromIndex, int toIndex)

Since: 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.

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

public int hashCode ()

Since: 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
  • this object's hash code.

public boolean intersects (BitSet bs)

Since: API Level 1

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

public boolean isEmpty ()

Since: API Level 1

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

public int length ()

Since: 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.

public int nextClearBit (int index)

Since: 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.

Throws
IndexOutOfBoundsException if index < 0.

public int nextSetBit (int index)

Since: 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.

Throws
IndexOutOfBoundsException if index < 0.

public void or (BitSet bs)

Since: API Level 1

Logically ors the bits of this BitSet with bs.

public void set (int index, boolean state)

Since: API Level 1

Sets the bit at index index to state.

Throws
IndexOutOfBoundsException if index < 0.

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

Since: API Level 1

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

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

public void set (int fromIndex, int toIndex)

Since: API Level 1

Sets the range of bits [fromIndex, toIndex).

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

public void set (int index)

Since: API Level 1

Sets the bit at index index to true.

Throws
IndexOutOfBoundsException if index < 0.

public int size ()

Since: 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.

public String toString ()

Since: 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
  • a printable representation of this object.

public void xor (BitSet bs)

Since: API Level 1

Logically xors the bits of this BitSet with bs.