Class BitSet
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 new
BitSetwith size equal to 64 bits.See also
-
#clear(int)
-
#set(int)
-
#clear()
-
#clear(int, int)
-
#set(int, boolean)
-
#set(int, int)
-
#set(int, int, boolean)
-
-
BitSet
public BitSet(int nbits) Create a new
BitSetwith 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
-
#clear(int)
-
#set(int)
-
#clear()
-
#clear(int, int)
-
#set(int, boolean)
-
#set(int, int)
-
#set(int, int, boolean)
-
-
Method Details
-
equals
Compares the argument to this
BitSetand returns whether they are equal. The object must be an instance ofBitSetwith the same bits set.Parameters
obj: theBitSetobject to compare.
Returns
-
hashCode
-
get
public boolean get(int pos) Retrieves the bit at index
pos. Grows theBitSetifpos > size.Parameters
pos: the index of the bit to be retrieved.
Returns
- Returns:
trueif the bit atposis set,falseotherwise.Throws
IndexOutOfBoundsException: ifposis negative.
See also
-
#clear(int)
-
#set(int)
-
#clear()
-
#clear(int, int)
-
#set(int, boolean)
-
#set(int, int)
-
#set(int, int, boolean)
-
get
Retrieves the bits starting from
pos1topos2and 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: @throws IndexOutOfBoundsException ifpos1orpos2is negative, or ifpos2is smaller thanpos1.
See also
- #get(int)
-
-
set
public void set(int pos) Sets the bit at index
posto 1. Grows theBitSetifpos > size.Parameters
pos: the index of the bit to set.
Throws
IndexOutOfBoundsException: ifposis negative.
See also
-
#clear(int)
-
#clear()
-
#clear(int, int)
-
set
public void set(int pos, boolean val) Sets the bit at index
postoval. 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(int)
-
-
set
public void set(int pos1, int pos2) Sets the bits starting from
pos1topos2. Grows theBitSetifpos2 > size.Parameters
-
pos1: beginning position. -
pos2: ending position.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifpos1orpos2is negative, or ifpos2is smaller thanpos1.
See also
- #set(int)
-
-
set
public void set(int pos1, int pos2, boolean val) Sets the bits starting from
pos1topos2to the givenval. Grows theBitSetifpos2 > size.Parameters
-
pos1: beginning position. -
pos2: ending position. -
val: value to set these bits.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifpos1orpos2is negative, or ifpos2is smaller thanpos1.
See also
- #set(int,int)
-
-
clear
public void clear()Clears all the bits in this
BitSet.See also
-
#clear(int)
-
#clear(int, int)
-
-
clear
public void clear(int pos) Clears the bit at index
pos. Grows theBitSetifpos > size.Parameters
pos: the index of the bit to clear.
Throws
IndexOutOfBoundsException: ifposis negative.
See also
- #clear(int, int)
-
clear
public void clear(int pos1, int pos2) Clears the bits starting from
pos1topos2. Grows theBitSetifpos2 > size.Parameters
-
pos1: beginning position. -
pos2: ending position.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifpos1orpos2is negative, or ifpos2is smaller thanpos1.
See also
- #clear(int)
-
-
flip
public void flip(int pos) Flips the bit at index
pos. Grows theBitSetifpos > size.Parameters
pos: the index of the bit to flip.
Throws
IndexOutOfBoundsException: ifposis negative.
See also
- #flip(int, int)
-
flip
public void flip(int pos1, int pos2) Flips the bits starting from
pos1topos2. Grows theBitSetifpos2 > size.Parameters
-
pos1: beginning position. -
pos2: ending position.
Throws
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifpos1orpos2is negative, or ifpos2is smaller thanpos1.
See also
- #flip(int)
-
-
intersects
Checks if these two
BitSets have at least one bit set to true in the same position.Parameters
bs:BitSetused to calculate the intersection.
Returns
- Returns:
trueif bs intersects with thisBitSet,falseotherwise.
-
and
Performs the logical AND of this
BitSetwith anotherBitSet. The values of thisBitSetare changed accordingly.Parameters
bs:BitSetto AND with.
See also
-
#or
-
#xor
-
andNot
Clears all bits in the receiver which are also set in the parameter
BitSet. The values of thisBitSetare changed accordingly.Parameters
bs:BitSetto ANDNOT with.
-
or
Performs the logical OR of this
BitSetwith anotherBitSet. The values of thisBitSetare changed accordingly.Parameters
bs:BitSetto OR with.
See also
-
#xor
-
#and
-
xor
Performs the logical XOR of this
BitSetwith anotherBitSet. The values of thisBitSetare changed accordingly.Parameters
bs:BitSetto XOR with.
See also
-
#or
-
#and
-
size
public int size()Returns the number of bits this
BitSethas.Returns
the number of bits contained in this
BitSet.See also
- #length
-
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 is
trueon 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 is
falseon or afterpos.Parameters
pos: the starting position (inclusive).
Returns
- 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 this
BitSetare set to false.Returns
- Returns:
trueif theBitSetis empty,falseotherwise.
-
cardinality
public int cardinality()Returns the number of bits that are
truein thisBitSet.Returns
the number of
truebits in the set.
-