Class BitSet
java.lang.Object
java.util.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.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidPerforms the logical AND of thisBitSetwith anotherBitSet.voidClears all bits in the receiver which are also set in the parameterBitSet.intReturns the number of bits that aretruein thisBitSet.voidclear()Clears all the bits in thisBitSet.voidclear(int pos) Clears the bit at indexpos.voidclear(int pos1, int pos2) Clears the bits starting frompos1topos2.booleanCompares the argument to thisBitSetand returns whether they are equal.voidflip(int pos) Flips the bit at indexpos.voidflip(int pos1, int pos2) Flips the bits starting frompos1topos2.booleanget(int pos) Retrieves the bit at indexpos.get(int pos1, int pos2) Retrieves the bits starting frompos1topos2and returns back a new bitset made of these bits.inthashCode()Computes the hash code for thisBitSet.booleanintersects(BitSet bs) Checks if these twoBitSets have at least one bit set to true in the same position.booleanisEmpty()Returns true if all the bits in thisBitSetare set to false.intlength()Returns the number of bits up to and including the highest bit set.intnextClearBit(int pos) Returns the position of the first bit that isfalseon or afterpos.intnextSetBit(int pos) Returns the position of the first bit that istrueon or afterpos.voidPerforms the logical OR of thisBitSetwith anotherBitSet.voidset(int pos) Sets the bit at indexposto 1.voidset(int pos, boolean val) Sets the bit at indexpostoval.voidset(int pos1, int pos2) Sets the bits starting frompos1topos2.voidset(int pos1, int pos2, boolean val) Sets the bits starting frompos1topos2to the givenval.intsize()Returns the number of bits thisBitSethas.toString()Returns a string containing a concise, human-readable description of the receiver.voidPerforms the logical XOR of thisBitSetwith anotherBitSet.
-
Constructor Details
-
BitSet
public BitSet()Create a newBitSetwith size equal to 64 bits.- See Also:
-
BitSet
public BitSet(int nbits) Create a newBitSetwith size equal to nbits. If nbits is not a multiple of 64, then create aBitSetwith size nbits rounded to the next closest multiple of 64.- Parameters:
nbits- the size of the bit set.- Throws:
NegativeArraySizeException- ifnbitsis negative.- See Also:
-
-
Method Details
-
equals
Compares the argument to thisBitSetand returns whether they are equal. The object must be an instance ofBitSetwith the same bits set. -
hashCode
-
get
public boolean get(int pos) Retrieves the bit at indexpos. Grows theBitSetifpos > size.- Parameters:
pos- the index of the bit to be retrieved.- Returns:
trueif the bit atposis set,falseotherwise.- Throws:
IndexOutOfBoundsException- ifposis negative.- See Also:
-
get
Retrieves the bits starting frompos1topos2and returns back a new bitset made of these bits. Grows theBitSetifpos2 > size.- Parameters:
pos1- beginning position.pos2- ending position.- Returns:
- new bitset of the range specified.
- Throws:
IndexOutOfBoundsException- ifpos1orpos2is negative, or ifpos2is smaller thanpos1.- See Also:
-
set
public void set(int pos) Sets the bit at indexposto 1. Grows theBitSetifpos > size.- Parameters:
pos- the index of the bit to set.- Throws:
IndexOutOfBoundsException- ifposis negative.- See Also:
-
set
public void set(int pos, boolean val) Sets the bit at indexpostoval. Grows theBitSetifpos > size.- Parameters:
pos- the index of the bit to set.val- value to set the bit.- Throws:
IndexOutOfBoundsException- ifposis negative.- See Also:
-
set
public void set(int pos1, int pos2) Sets the bits starting frompos1topos2. Grows theBitSetifpos2 > size.- Parameters:
pos1- beginning position.pos2- ending position.- Throws:
IndexOutOfBoundsException- ifpos1orpos2is negative, or ifpos2is smaller thanpos1.- See Also:
-
set
public void set(int pos1, int pos2, boolean val) Sets the bits starting frompos1topos2to the givenval. Grows theBitSetifpos2 > size.- Parameters:
pos1- beginning position.pos2- ending position.val- value to set these bits.- Throws:
IndexOutOfBoundsException- ifpos1orpos2is negative, or ifpos2is smaller thanpos1.- See Also:
-
clear
public void clear()Clears all the bits in thisBitSet.- See Also:
-
clear
public void clear(int pos) Clears the bit at indexpos. Grows theBitSetifpos > size.- Parameters:
pos- the index of the bit to clear.- Throws:
IndexOutOfBoundsException- ifposis negative.- See Also:
-
clear
public void clear(int pos1, int pos2) Clears the bits starting frompos1topos2. Grows theBitSetifpos2 > size.- Parameters:
pos1- beginning position.pos2- ending position.- Throws:
IndexOutOfBoundsException- ifpos1orpos2is negative, or ifpos2is smaller thanpos1.- See Also:
-
flip
public void flip(int pos) Flips the bit at indexpos. Grows theBitSetifpos > size.- Parameters:
pos- the index of the bit to flip.- Throws:
IndexOutOfBoundsException- ifposis negative.- See Also:
-
flip
public void flip(int pos1, int pos2) Flips the bits starting frompos1topos2. Grows theBitSetifpos2 > size.- Parameters:
pos1- beginning position.pos2- ending position.- Throws:
IndexOutOfBoundsException- ifpos1orpos2is negative, or ifpos2is smaller thanpos1.- See Also:
-
intersects
Checks if these twoBitSets have at least one bit set to true in the same position.- Parameters:
bs-BitSetused to calculate the intersection.- Returns:
trueif bs intersects with thisBitSet,falseotherwise.
-
and
Performs the logical AND of thisBitSetwith anotherBitSet. The values of thisBitSetare changed accordingly.- Parameters:
bs-BitSetto AND with.- See Also:
-
andNot
Clears all bits in the receiver which are also set in the parameterBitSet. The values of thisBitSetare changed accordingly.- Parameters:
bs-BitSetto ANDNOT with.
-
or
Performs the logical OR of thisBitSetwith anotherBitSet. The values of thisBitSetare changed accordingly.- Parameters:
bs-BitSetto OR with.- See Also:
-
xor
Performs the logical XOR of thisBitSetwith anotherBitSet. The values of thisBitSetare changed accordingly.- Parameters:
bs-BitSetto XOR with.- See Also:
-
size
public int size()Returns the number of bits thisBitSethas.- 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
-
nextSetBit
public int nextSetBit(int pos) Returns the position of the first bit that istrueon or afterpos.- Parameters:
pos- the starting position (inclusive).- Returns:
- -1 if there is no bits that are set to
trueon or afterpos.
-
nextClearBit
public int nextClearBit(int pos) Returns the position of the first bit that isfalseon or afterpos.- Parameters:
pos- the starting position (inclusive).- Returns:
- the position of the next bit set to
false, even if it is further than thisBitSet's size.
-
isEmpty
public boolean isEmpty()Returns true if all the bits in thisBitSetare set to false.- Returns:
trueif theBitSetis empty,falseotherwise.
-
cardinality
public int cardinality()Returns the number of bits that aretruein thisBitSet.- Returns:
- the number of
truebits in the set.
-