Skip to content

Most visited

Recently visited

navigation
Added in API level 1

BitSet

public class BitSet
extends Object implements Serializable, Cloneable

java.lang.Object
   ↳ java.util.BitSet


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.

Inherited methods

From class java.lang.Object

Public constructors

BitSet

Added in API level 1
BitSet ()

Creates a new BitSet with size equal to 64 bits.

BitSet

Added in API level 1
BitSet (int bitCount)

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

and

Added in API level 1
void and (BitSet bs)

Logically ands the bits of this BitSet with bs.

Parameters
bs BitSet

andNot

Added in API level 1
void andNot (BitSet bs)

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

Parameters
bs BitSet

cardinality

Added in API level 1
int cardinality ()

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

Returns
int

clear

Added in API level 1
void clear (int index)

Clears the bit at index index.

Parameters
index int
Throws
IndexOutOfBoundsException if index < 0.

clear

Added in API level 1
void clear ()

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.

clear

Added in API level 1
void clear (int fromIndex, 
                int toIndex)

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.

clone

Added in API level 1
Object clone ()

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.

equals

Added in API level 1
boolean equals (Object o)

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.

flip

Added in API level 1
void flip (int index)

Flips the bit at index index.

Parameters
index int
Throws
IndexOutOfBoundsException if index < 0.

flip

Added in API level 1
void flip (int fromIndex, 
                int toIndex)

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.

get

Added in API level 1
boolean get (int index)

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

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

get

Added in API level 1
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.

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

hashCode

Added in API level 1
int hashCode ()

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.

intersects

Added in API level 1
boolean intersects (BitSet bs)

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

Parameters
bs BitSet
Returns
boolean

isEmpty

Added in API level 1
boolean isEmpty ()

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

Returns
boolean

length

Added in API level 1
int length ()

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

Returns
int

nextClearBit

Added in API level 1
int nextClearBit (int index)

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.

nextSetBit

Added in API level 1
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.

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

or

Added in API level 1
void or (BitSet bs)

Logically ors the bits of this BitSet with bs.

Parameters
bs BitSet

previousClearBit

Added in API level 19
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.

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

previousSetBit

Added in API level 19
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.

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

set

Added in API level 1
void set (int index, 
                boolean state)

Sets the bit at index index to state.

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

set

Added in API level 1
void set (int fromIndex, 
                int toIndex, 
                boolean state)

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.

set

Added in API level 1
void set (int fromIndex, 
                int toIndex)

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.

set

Added in API level 1
void set (int index)

Sets the bit at index index to true.

Parameters
index int
Throws
IndexOutOfBoundsException if index < 0.

size

Added in API level 1
int size ()

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

toByteArray

Added in API level 19
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.

Returns
byte[]

toLongArray

Added in API level 19
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.

Returns
long[]

toString

Added in API level 1
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. For example: "{0,1,8"}.

Returns
String a printable representation of this object.

valueOf

Added in API level 19
BitSet valueOf (byte[] bytes)

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

Parameters
bytes byte
Returns
BitSet

valueOf

Added in API level 19
BitSet valueOf (ByteBuffer byteBuffer)

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

valueOf

Added in API level 19
BitSet valueOf (LongBuffer longBuffer)

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

valueOf

Added in API level 19
BitSet valueOf (long[] longs)

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

xor

Added in API level 1
void xor (BitSet bs)

Logically xors the bits of this BitSet with bs.

Parameters
bs BitSet
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.