简体   繁体   English

Java BitSet的大小大于构造函数中设置的nbits

[英]Java BitSet size larger then nbits set in Constructor

I am creating a BitSet with a fixed number of bits. 我正在创建一个具有固定位数的BitSet。 In this case the length of my String holding the binary representation is 508 characters long. 在这种情况下,包含二进制表示形式的String的长度为508个字符。

So I create BitSet the following way: 因此,我通过以下方式创建BitSet:

BitSet bs = new BitSet(binary.length());
// binary.length() = 508

But looking at the size of bs I always get a size of 512. I can see that there are always 4 Bits with value of 0 appended at the end. 但是看bs的大小,我总是得到512的大小。我可以看到总是在末尾附加4位,值为0。

Maybe there is some misunderstanding of the following documentation: 可能对以下文档有一些误解:

BitSet(int nbits) BitSet(整数位)

Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1. 创建一个位集合,其初始大小足够大,以明确表示索引范围在0到nbits-1之间的位。

Is it that BitSet always enhances its size so that its size is powers of 2 or why is it larger? 是BitSet总是增加其大小以使其大小为2的幂还是为什么更大?

The number of bits in the constructor is a sizing hint, not a limit on the number of bits allowed. 构造函数中的位数只是一个大小提示,而不是对所允许位数的限制。 The size() of a Bitset is effectively its current capacity , though the specification is rather more rubbery than that. Bitset的size()实际上是其当前容量 ,尽管规范比之更具弹性。

So I can't rely on the size if I get passed another bitset? 因此,如果我通过另一个位集,我就不能依靠大小吗? There may also be some bits appended or it can be longer than "expected" ? 可能还会附加一些位,或者可能比“预期”长一些?

Correct, and yes. 正确,是的。

If you want the logical size (ie the highest bit index that is set) use the length() method, not the size() method. 如果要逻辑大小(即设置的最高位索引),请使用length()方法,而不要使用size()方法。

If length() gives me the highest bit set, this can't help in every situation. 如果length()给了我最高的设置,这在每种情况下都无济于事。 Because "my" highest bit on position 508 can also be 0. 因为位置508上的“我”最高位也可以为0。

In this case "set" means "set to 1 / true". 在这种情况下,“设置”表示“设置为1 / true”。 So if your highest bit (at position 508) is a zero, the length() will be less than 508. I'm not sure if that will help. 因此,如果您的最高位(在位置508)为零,则length()将小于508。我不确定这是否有帮助。 But if you have a concept of a highest bit position that is defined then you need to represent that position as a separate value. 但是,如果定义了最高位位置的概念,则需要将该位置表示为单独的值。

A Bitset is actually modelled as a potentially infinite array of bits which is default initialized to all zeros. 实际上,将位集建模为潜在的无限位数组,其默认初始化为全零。 (That's why there is no "flip the entire Bitset" operation. It would use a huge amount of storage.) (这就是为什么没有“翻转整个Bitset”操作的原因。它将占用大量存储空间。)

According to the documentation , the actual size in memory is implementation dependent, so you can't really know for sure what size() you're going to get. 根据文档 ,内存的实际大小取决于实现,因此您无法确定要获得的size() You as a user shouldn't have to worry about it, though, because the length() of the BitSet is always accurate - even if the size in memory is larger, it returns the number of bits actually in use. 但是,作为用户,您不必担心,因为BitSet的length()始终是准确的-即使内存中的大小较大,它也会返回实际使用的位数。

Since the BitSet can automatically grow to accomodate any data added to it, I wouldn't be surprised if it uses a growth strategy that's similar to lists, which tend to use increasing powers of two. 由于BitSet可以自动增长以适应添加到其中的任何数据,因此,如果它使用与列表相似的增长策略(往往使用递增的2的幂),我不会感到惊讶。 But as said, that fact is an implementation detail, and it might not be the same everywhere and every time. 但是,正如所说的,这是一个实现细节,在每个地方和每次都可能并不相同。

That's just a hint for a collection (this applies to all collections I think) so it don't have to resize itself after adding elements. 这只是一个集合的提示(这适用于我认为的所有集合),因此添加元素后不必调整自身大小。 For instance, if you know that your collection will hold 100 elements at maximum, you can set it's size to 100 and no resize will be made which is better for performance. 例如,如果您知道您的集合最多可容纳100个元素,则可以将其大小设置为100,并且不会进行任何调整以提高性能。

The BitSet size will be set to the first multiple of 64 that is equal to or greater than the number you use for 'size'. BitSet大小将被设置为64的第一个倍数,该值等于或大于您用于“大小”的数字。 If you specify a 'size' of 508, you will get a BitSet with an actual size of 512, which is the next highest multiple of 64. 如果将“大小”指定为508,则将获得一个实际大小为512的BitSet,这是64的下一个最大倍数。

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

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