/** BitSets is packed into arrays of "words." Currently a word is * a long, which consists of up to 6 bits, requiring. * The choice of word size is determined purely by performance concerns. */ Private Final Static intAddress_bits_per_word = 6; Private Final Static intBits_per_word = 1 <<Address_bits_per_word; Private Final Static intBit_index_mask = bits_per_word-1; /*used to shift left or right for a partial word mask*/ Private Static Final LongWord_mask = 0xffffffffffffffffL;/*** The internal field corresponding to the Serialfield "bits". */ Private Long[] words; /*** The number of words in the logical size of this BitSet. */ Private transient intWordsinuse = 0;/*** Given a bit index, return Word index containing it. */ Private Static intWordindex (intBitindex) { returnBitindex >>Address_bits_per_word; }/*** Creates a new bit set. All bits is initially {@codefalse}. */ PublicBitSet () {initwords (Bits_per_word); Sizeissticky=false; }/*** Creates a bit set whose initial size is large enough to explicitly * represent bits with indices in the RAN GE {@code0} through * {@codenbits-1}. All bits is initially {@codefalse}. * * @paramnbits The initial size of the bit set *@throwsNegativearraysizeexception If the specified initial size * is negative*/ Public BitSet (int nbits) { //nbits can ' t be negative; size 0 is OK if(Nbits < 0) Throw NewNegativearraysizeexception ("Nbits < 0:" +nbits); Initwords (nbits); Sizeissticky=true; } Private voidInitwords (intnbits) {Words=New Long[Wordindex (nbits-1) + 1]; } /*** Sets the bit at the specified index to {@codetrue}. * * @parambitindex a bit index *@throwsindexoutofboundsexception If the specified index is negative *@sinceJDK1.0*/ Public voidSetintBitindex) { if(Bitindex < 0) Throw NewIndexoutofboundsexception ("Bitindex < 0:" +Bitindex); intWordindex =Wordindex (Bitindex); Expandto (Wordindex); Words[wordindex]|= (1L << bitindex);//restores invariantscheckinvariants (); } /*** Sets the bit at the specified index to the specified value. * * @parambitindex a bit index *@paramvalue A Boolean value to set *@throwsindexoutofboundsexception If the specified index is negative *@since1.4*/ Public voidSetintBitindex,Booleanvalue) { if(value) set (Bitindex); ElseClear (Bitindex); }/*** Sets the bit specified by the index to {@codefalse}. * * @paramBitindex the index of the bit to be cleared *@throwsindexoutofboundsexception If the specified index is negative *@sinceJDK1.0*/ Public voidClearintBitindex) { if(Bitindex < 0) Throw NewIndexoutofboundsexception ("Bitindex < 0:" +Bitindex); intWordindex =Wordindex (Bitindex); if(Wordindex >=wordsinuse)return; Words[wordindex]&= ~ (1L <<Bitindex); Recalculatewordsinuse (); Checkinvariants (); }
[email protected] java bit self practice# #Q &a: Why int to initialize size of bitvector? Long, even the BigInteger strategy