Java Reference
In-Depth Information
BitSet
provides a pair of constructors for initializing a
BitSet
instance:
BitSet()
initializestheinstancetoinitiallystoreanimplementation-dependentnum-
ber of bits, whereas
BitSet(int nbits)
initializes the instance to initially store
nbits
bits.
BitSet
also provides various methods, including the following:
•
void and(BitSet bs)
bitwise ANDs this bitset with
bs
. This bitset is
modifiedsuchthatabitissetto1whenitandthebitatthesamepositionin
bs
are 1.
•
void andNot(BitSet bs)
setsallthebitsinthisbitsetto0whosecor-
responding bits are set to 1 in
bs
.
•
void clear()
sets all the bits in this bitset to 0.
•
Object clone()
clones this bitset to produce a new bitset. The clone has
exactly the same bits set to one as this bitset.
•
boolean get(int bitIndex)
returnsthevalueofthisbitset's bit,asa
Booleantrue/falsevalue(truefor1,falsefor0)atthezero-based
bitIndex
.
Thismethodthrows
IndexOutOfBoundsException
when
bitIndex
is
less than 0.
•
int length()
returnsthe“logicalsize”ofthisbitset,whichistheindexof
the highest 1 bit plus 1, or 0 when this bitset contains no 1 bits.
•
void or(BitSet bs)
bitwiseinclusiveORsthisbitsetwith
bs
.Thisbitset
ismodifiedsuchthatabitissetto1whenitorthebitatthesamepositionin
bs
is 1, or when both bits are 1.
•
void set(int bitIndex, boolean value)
setsthebitatthezero-
based
bitIndex
to
value
(true is converted to 1; false is converted to 0).
Thismethodthrows
IndexOutOfBoundsException
when
bitIndex
is
less than 0.
•
int size()
returns the number of bits that are being used by this bitset to
represent bit values.
•
String toString()
returns a string representation of this bitset in terms
of the positions of bits that are 1; for example,
{4, 5, 9, 10}
.
•
void xor(BitSet set)
bitwise exclusive ORsthisbitset with
bs
.This
bitsetismodifiedsuchthatabitissetto1wheneitheritorthebitatthesame
position in
bs
(but not both) is 1.