public class BitSet extends Object
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 and Description |
---|
BitSet()
Create a new
BitSet with size equal to 64 bits. |
BitSet(int nbits)
Create a new
BitSet with size equal to nbits. |
Modifier and Type | Method and Description |
---|---|
void |
and(BitSet bs)
Performs the logical AND of this
BitSet with another
BitSet . |
void |
andNot(BitSet bs)
Clears all bits in the receiver which are also set in the parameter
BitSet . |
int |
cardinality()
Returns the number of bits that are
true in this BitSet . |
void |
clear()
Clears all the bits in this
BitSet . |
void |
clear(int pos)
Clears the bit at index
pos . |
void |
clear(int pos1,
int pos2)
Clears the bits starting from
pos1 to pos2 . |
boolean |
equals(Object obj)
Compares the argument to this
BitSet and returns whether they are
equal. |
void |
flip(int pos)
Flips the bit at index
pos . |
void |
flip(int pos1,
int pos2)
Flips the bits starting from
pos1 to pos2 . |
boolean |
get(int pos)
Retrieves the bit at index
pos . |
BitSet |
get(int pos1,
int pos2)
Retrieves the bits starting from
pos1 to pos2 and returns
back a new bitset made of these bits. |
int |
hashCode()
Computes the hash code for this
BitSet . |
boolean |
intersects(BitSet bs)
Checks if these two
BitSet s have at least one bit set to true in the same
position. |
boolean |
isEmpty()
Returns true if all the bits in this
BitSet are set to false. |
int |
length()
Returns the number of bits up to and including the highest bit set.
|
int |
nextClearBit(int pos)
Returns the position of the first bit that is
false on or after pos . |
int |
nextSetBit(int pos)
Returns the position of the first bit that is
true on or after pos . |
void |
or(BitSet bs)
Performs the logical OR of this
BitSet with another BitSet . |
void |
set(int pos)
Sets the bit at index
pos to 1. |
void |
set(int pos,
boolean val)
Sets the bit at index
pos to val . |
void |
set(int pos1,
int pos2)
Sets the bits starting from
pos1 to pos2 . |
void |
set(int pos1,
int pos2,
boolean val)
Sets the bits starting from
pos1 to pos2 to the given
val . |
int |
size()
Returns the number of bits this
BitSet has. |
String |
toString()
Returns a string containing a concise, human-readable description of the
receiver.
|
void |
xor(BitSet bs)
Performs the logical XOR of this
BitSet with another BitSet . |
public BitSet()
BitSet
with size equal to 64 bits.public BitSet(int nbits)
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.nbits
- the size of the bit set.NegativeArraySizeException
- if nbits
is negative.clear(int)
,
set(int)
,
clear()
,
clear(int, int)
,
set(int, boolean)
,
set(int, int)
,
set(int, int, boolean)
public boolean equals(Object obj)
BitSet
and returns whether they are
equal. The object must be an instance of BitSet
with the same
bits set.equals
in class Object
obj
- the BitSet
object to compare.boolean
indicating whether or not this BitSet
and
obj
are equal.hashCode()
public int hashCode()
BitSet
. If two BitSet
s are equal
the have to return the same result for hashCode()
.hashCode
in class Object
int
representing the hash code for this bit
set.equals(java.lang.Object)
,
Hashtable
public boolean get(int pos)
pos
. Grows the BitSet
if
pos > size
.pos
- the index of the bit to be retrieved.true
if the bit at pos
is set,
false
otherwise.IndexOutOfBoundsException
- if pos
is negative.clear(int)
,
set(int)
,
clear()
,
clear(int, int)
,
set(int, boolean)
,
set(int, int)
,
set(int, int, boolean)
public BitSet get(int pos1, int pos2)
pos1
to pos2
and returns
back a new bitset made of these bits. Grows the BitSet
if
pos2 > size
.pos1
- beginning position.pos2
- ending position.IndexOutOfBoundsException
- if pos1
or pos2
is negative, or if
pos2
is smaller than pos1
.get(int)
public void set(int pos)
pos
to 1. Grows the BitSet
if
pos > size
.pos
- the index of the bit to set.IndexOutOfBoundsException
- if pos
is negative.clear(int)
,
clear()
,
clear(int, int)
public void set(int pos, boolean val)
pos
to val
. Grows the
BitSet
if pos > size
.pos
- the index of the bit to set.val
- value to set the bit.IndexOutOfBoundsException
- if pos
is negative.set(int)
public void set(int pos1, int pos2)
pos1
to pos2
. Grows the
BitSet
if pos2 > size
.pos1
- beginning position.pos2
- ending position.IndexOutOfBoundsException
- if pos1
or pos2
is negative, or if
pos2
is smaller than pos1
.set(int)
public void set(int pos1, int pos2, boolean val)
pos1
to pos2
to the given
val
. Grows the BitSet
if pos2 > size
.pos1
- beginning position.pos2
- ending position.val
- value to set these bits.IndexOutOfBoundsException
- if pos1
or pos2
is negative, or if
pos2
is smaller than pos1
.set(int,int)
public void clear()
BitSet
.clear(int)
,
clear(int, int)
public void clear(int pos)
pos
. Grows the BitSet
if
pos > size
.pos
- the index of the bit to clear.IndexOutOfBoundsException
- if pos
is negative.clear(int, int)
public void clear(int pos1, int pos2)
pos1
to pos2
. Grows the
BitSet
if pos2 > size
.pos1
- beginning position.pos2
- ending position.IndexOutOfBoundsException
- if pos1
or pos2
is negative, or if
pos2
is smaller than pos1
.clear(int)
public void flip(int pos)
pos
. Grows the BitSet
if
pos > size
.pos
- the index of the bit to flip.IndexOutOfBoundsException
- if pos
is negative.flip(int, int)
public void flip(int pos1, int pos2)
pos1
to pos2
. Grows the
BitSet
if pos2 > size
.pos1
- beginning position.pos2
- ending position.IndexOutOfBoundsException
- if pos1
or pos2
is negative, or if
pos2
is smaller than pos1
.flip(int)
public boolean intersects(BitSet bs)
BitSet
s have at least one bit set to true in the same
position.bs
- BitSet
used to calculate the intersection.true
if bs intersects with this BitSet
,
false
otherwise.public void and(BitSet bs)
BitSet
with another
BitSet
. The values of this BitSet
are changed accordingly.bs
- BitSet
to AND with.or(java.util.BitSet)
,
xor(java.util.BitSet)
public void andNot(BitSet bs)
BitSet
. The values of this BitSet
are changed accordingly.bs
- BitSet
to ANDNOT with.public void or(BitSet bs)
BitSet
with another BitSet
.
The values of this BitSet
are changed accordingly.bs
- BitSet
to OR with.xor(java.util.BitSet)
,
and(java.util.BitSet)
public void xor(BitSet bs)
BitSet
with another BitSet
.
The values of this BitSet
are changed accordingly.bs
- BitSet
to XOR with.or(java.util.BitSet)
,
and(java.util.BitSet)
public int size()
BitSet
has.BitSet
.length()
public int length()
BitSet
.public String toString()
public int nextSetBit(int pos)
true
on or after pos
.pos
- the starting position (inclusive).true
on or after pos
.public int nextClearBit(int pos)
false
on or after pos
.pos
- the starting position (inclusive).false
, even if it is further
than this BitSet
's size.public boolean isEmpty()
BitSet
are set to false.true
if the BitSet
is empty,
false
otherwise.public int cardinality()
true
in this BitSet
.true
bits in the set.