简体   繁体   English

在Java中创建特定长度的BitSet

[英]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. 我有一个要在其中执行OR,AND,set等操作的函数。 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). 我首先想到了一个布尔数组,但是问题是我无法对它们执行AND和OR操作(但是我可以将特定元素设置为true)。 I also tried to used BiSet, but the problem is that the minimum size of BitSet is 64 (bits). 我也尝试过使用BiSet,但是问题是BitSet的最小大小为64(位)。 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. 我希望它存储特定数量的位(例如15),以后可以从中进行迭代以获取其包含。 In C#, there is the BitArray Class that deals with such problem; 在C#中,存在处理此类问题的BitArray类。 Java does not seem to have such. Java似乎没有这种功能。 Any suggestions for this problem will highly be appreciated 对于这个问题的任何建议将不胜感激

I think you are mistaken. 我想你错了。 java.util.Bitset allows for any number of bits; java.util.Bitset允许任意数量的位; there is no "only > 64 bits" limitation. 没有“仅> 64位”限制。 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. 但没有任何地方说您只能使用64位,128等。

In other words: when you setup a Bitset for 15 bits, it will allocate long[1] ; 换句话说:当您将一个Bitset设置为15位时,它将分配long[1] same for 63 bits; 相同的63位; and if you need 70 bits, it will allocate long[2] . 如果需要70位,它将分配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. 而且我看不到任何其他实现方式都可以大大提高效率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM