bitvector@ java bit self practice# #Q &a: Why int to initialize size of bitvector? Long, even the BigInteger strategy

Source: Internet
Author: User
Tags bit set bitset


/** 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.