user3841581
user3841581

Reputation: 2747

Creating a BitSet of specific length in java

I have a function inside which I would like to performs operations such OR, AND, set and others. I first thought about an array of boolean, but the problem with that is that I can not perform the AND and OR operations on them (I could however set a particular elements to true). I also tried to used BiSet, but the problem is that the minimum size of BitSet is 64 (bits). I would like it to store a specific number of bits (15 for example) from which I can iterate through later on to get their contains. In C#, there is the BitArray Class that deals with such problem; Java does not seem to have such. Any suggestions for this problem will highly be appreciated

Upvotes: 1

Views: 1090

Answers (1)

GhostCat
GhostCat

Reputation: 140417

I think you are mistaken. java.util.Bitset allows for any number of bits; there is no "only > 64 bits" limitation. Yes, the bitset is probably internally using long values to store those bits; but nowhere does it say that you can only use 64 bit, 128, and so.

In other words: when you setup a Bitset for 15 bits, it will allocate long[1]; same for 63 bits; and if you need 70 bits, it will allocate long[2].

Long story short: this class is exactly what you are looking for; and I don't see that any other implementation could be vastly more efficient or so.

Upvotes: 5

Related Questions