public class BitSet

  1. Object
  2. BitSet
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.

Constructors

public BitSet()Create a new BitSet with size equal to 64 bits.
public BitSet(int nbits)Create a new BitSet with size equal to nbits.

Methods

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

Inherited methods

Constructor details

BitSet

public BitSet()
Create a new BitSet with size equal to 64 bits.

BitSet

public BitSet(int nbits)
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 int
the size of the bit set.

Throws

NegativeArraySizeException
if nbits is negative.

Method details

equals

public boolean equals(Object obj)
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 Object
the BitSet object to compare.

Returns

a boolean indicating whether or not this BitSet and obj are equal.

See also

hashCode

public int hashCode()
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.

get

public boolean get(int pos)
Retrieves the bit at index pos. Grows the BitSet if pos > size.

Parameters

pos int
the index of the bit to be retrieved.

Returns

true if the bit at pos is set, false otherwise.

Throws

IndexOutOfBoundsException
if pos is negative.

get

public BitSet get(int pos1, int pos2)
Retrieves the bits starting from pos1 to pos2 and returns back a new bitset made of these bits. Grows the BitSet if pos2 > size.

Parameters

pos1 int
beginning position.
pos2 int
ending position.

Returns

new bitset of the range specified.

Throws

IndexOutOfBoundsException
if pos1 or pos2 is negative, or if pos2 is smaller than pos1.

See also

set

public void set(int pos)
Sets the bit at index pos to 1. Grows the BitSet if pos > size.

Parameters

pos int
the index of the bit to set.

Throws

IndexOutOfBoundsException
if pos is negative.

set

public void set(int pos, boolean val)
Sets the bit at index pos to val. Grows the BitSet if pos > size.

Parameters

pos int
the index of the bit to set.
val boolean
value to set the bit.

Throws

IndexOutOfBoundsException
if pos is negative.

See also

set

public void set(int pos1, int pos2)
Sets the bits starting from pos1 to pos2. Grows the BitSet if pos2 > size.

Parameters

pos1 int
beginning position.
pos2 int
ending position.

Throws

IndexOutOfBoundsException
if pos1 or pos2 is negative, or if pos2 is smaller than pos1.

See also

set

public void set(int pos1, int pos2, boolean val)
Sets the bits starting from pos1 to pos2 to the given val. Grows the BitSet if pos2 > size.

Parameters

pos1 int
beginning position.
pos2 int
ending position.
val boolean
value to set these bits.

Throws

IndexOutOfBoundsException
if pos1 or pos2 is negative, or if pos2 is smaller than pos1.

clear

public void clear()
Clears all the bits in this BitSet.

clear

public void clear(int pos)
Clears the bit at index pos. Grows the BitSet if pos > size.

Parameters

pos int
the index of the bit to clear.

Throws

IndexOutOfBoundsException
if pos is negative.

clear

public void clear(int pos1, int pos2)
Clears the bits starting from pos1 to pos2. Grows the BitSet if pos2 > size.

Parameters

pos1 int
beginning position.
pos2 int
ending position.

Throws

IndexOutOfBoundsException
if pos1 or pos2 is negative, or if pos2 is smaller than pos1.

See also

flip

public void flip(int pos)
Flips the bit at index pos. Grows the BitSet if pos > size.

Parameters

pos int
the index of the bit to flip.

Throws

IndexOutOfBoundsException
if pos is negative.

flip

public void flip(int pos1, int pos2)
Flips the bits starting from pos1 to pos2. Grows the BitSet if pos2 > size.

Parameters

pos1 int
beginning position.
pos2 int
ending position.

Throws

IndexOutOfBoundsException
if pos1 or pos2 is negative, or if pos2 is smaller than pos1.

See also

intersects

public boolean intersects(BitSet bs)
Checks if these two BitSets have at least one bit set to true in the same position.

Parameters

bs BitSet
BitSet used to calculate the intersection.

Returns

true if bs intersects with this BitSet, false otherwise.

and

public void and(BitSet bs)
Performs the logical AND of this BitSet with another BitSet. The values of this BitSet are changed accordingly.

Parameters

bs BitSet
BitSet to AND with.

See also

andNot

public void andNot(BitSet bs)
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
BitSet to ANDNOT with.

or

public void or(BitSet bs)
Performs the logical OR of this BitSet with another BitSet. The values of this BitSet are changed accordingly.

Parameters

bs BitSet
BitSet to OR with.

See also

xor

public void xor(BitSet bs)
Performs the logical XOR of this BitSet with another BitSet. The values of this BitSet are changed accordingly.

Parameters

bs BitSet
BitSet to XOR with.

See also

size

public int size()
Returns the number of bits this BitSet has.

Returns

the number of bits contained in this BitSet.

See also

length

public int length()
Returns the number of bits up to and including the highest bit set.

Returns

the length of the BitSet.

toString

public String toString()
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.

nextSetBit

public int nextSetBit(int pos)
Returns the position of the first bit that is true on or after pos.

Parameters

pos int
the starting position (inclusive).

Returns

-1 if there is no bits that are set to true on or after pos.

nextClearBit

public int nextClearBit(int pos)
Returns the position of the first bit that is false on or after pos.

Parameters

pos int
the starting position (inclusive).

Returns

the position of the next bit set to false, even if it is further than this BitSet’s size.

isEmpty

public boolean isEmpty()
Returns true if all the bits in this BitSet are set to false.

Returns

true if the BitSet is empty, false otherwise.

cardinality

public int cardinality()
Returns the number of bits that are true in this BitSet.

Returns

the number of true bits in the set.