Java – check that all bits in the BitSet are set to true

I use BitSet in my application and want to check a method if all used bits in BitSet are set to true Now, I know that the method isempty () checks whether all bits are set to false, but I can't seem to find a positive situation I know I can do something like somebitset cardinality()== someBitSet. Size () is such a thing, but it looks clumsy What have I missed, or is there a clear reason why such an approach has not been implemented, but the opposite is true?

Solution

There is nothing like "all bits in BitSet", because you can set a bit larger than the maximum bit set so far at any time Suppose you want to keep up to 10 values in BitSet So you set 10 bits and want to check if all this is true But BitSet didn't know you had only 10 How about you have more? Next time you can call BitSet Set (10000) it will work (BitSet will automatically resize)

Notice that BitSet Size () is not very useful in general: it's about memory consumption The current implementation is always a multiple of 64, so if you have only 10 different states, somebitset cardinality()== someBitSet. Size() will always return false Even if you created a BitSet with a new BitSet (10) The constructor parameter is just a required initial capacity (as in ArrayList) It is only used as a performance cue

From a performance point of view, the best solution is to check nextclearbit (0) > = mylength, where mylength is the maximum number of values you want to store in BitSet (you should keep it yourself) If the result is false, this may be faster than cardinality ()

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>