Java Bitset usage Scenarios and examples

Source: Internet
Author: User
Tags bit set bitset

First, what is Bitset?

Note: The following content is from the JDK API:

The Bitset class implements a bit vector that grows on demand. Each component of a bit set has a Boolean value. The bits of the bitset are indexed with nonnegative integers. Each indexed bit can be tested, set, or cleared. You can use one BitSet to modify the contents of another BitSet through logical AND logical OR logical XOR or manipulation.

By default, all bits in the set have an initial value of false.

Each bit set has a current size, which is the number of bits of the space currently used by the bit set. Note that this size is related to the implementation of bit set, so it may change depending on the implementation. The length of a bit set is related to the logical length of the bit set and is defined in relation to the implementation.

Second, Java Bitset implementation principle

In Java, the implementation of Bitset is in the Java.util package:

 Public classBitSetImplementscloneable, java.io.Serializable {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; Private Static Finalobjectstreamfield[] Serialpersistentfields =     {        NewObjectstreamfield ("bits",Long[].class),    }; /*** The internal field corresponding to the Serialfield "bits". */    Private Long[] words; .....}

As you can see, the underlying implementation of Bitset is to use a long array as the internal storage structure, so the size of the Bitset is an integer multiple of the long type size (64-bit).

It has two constructors:

1, BitSet (): Create a new bit set, the default size is 64 bits.

Public
{ initwords (bits_per_word); false ;}

2, BitSet (int nbits):创建一个位set,它的初始大小足以显式表示索引范围在 0 到 nbits-1 的位。

     Public BitSet (int  nbits)     {        //  nbits can ' t be negative; size 0 is OK        if
     (Nbits < 0)            thrownew negativearraysizeexception ("Nbits < 0:" +  nbits);        Initwords (nbits);         true ;    }

Note:

1, if the initialization size of the Bitset is specified, then it will be normalized to a greater than or equal to the number of the integer multiples of 64. For example , 64 bits, the size of the Bitset is 1 long, and 65 bits, the Bitset size is 2 long, that is, 128 bits. Making such a provision is primarily for memory alignment, while avoiding the need to avoid handling special cases and simplifying the program.

2:bitset Size Method: Returns the number of bits of space that are actually used when this BitSet represents a bit value, which is an integer multiple of 64

Length method: Returns the "logical size" of this BitSet: index of the highest set bit in BitSet plus 1

third, the use of the scene

A common application scenario is to do some statistical work on large amounts of data, such as log analysis, user statistics, and so on.

Before the internship interview in Ali was asked a question: there are 10 million random numbers, the range of random numbers between 1 to 100 million. Now it is required to write an algorithm that does not have a number in the random number between 1 and 100 million?

The code examples are as follows:

 Public classalibaba{ Public Static voidMain (string[] args) {random random=NewRandom (); List<Integer> list=NewArraylist<>();  for(inti=0;i<10000000;i++)        {            intRandomresult=random.nextint (100000000);        List.add (Randomresult); } System.out.println ("Random number produced");  for(intI=0;i<list.size (); i++) {System.out.println (List.get (i)); } BitSet BitSet=NewBitSet (100000000);  for(inti=0;i<10000000;i++) {Bitset.set (List.get (i)); } System.out.println ("0~1 billion does not have in the above random number" +bitset.size ());  for(inti = 0; i < 100000000; i++)        {            if(!Bitset.get (i))            {System.out.println (i); }        }         }}
Iv. references

1, http://blog.csdn.net/haojun186/article/details/8482343

2, http://www.open-open.com/lib/view/open1406379530429.html

Java Bitset usage Scenarios and examples

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.