简体   繁体   English

为什么我的BitSet的大小为0?

[英]Why does my BitSet have a size of 0?

Here is my code. 这是我的代码。 What I am trying to do is process the bytes from my ByteBuffer using a BitSet. 我想做的是使用BitSet处理来自ByteBuffer的字节。 The ByteBuffer is loaded from DynamoDB(With the DynamoDBMapper) ByteBuffer是从DynamoDB(使用DynamoDBMapper)加载的

    ByteBuffer buffer = .......
    System.out.println("Array length is " + buffer.array().length);
    BitSet bitSet = BitSet.valueOf(buffer.array());
    System.out.println("Bit set size is " + bitSet.size());

When I execute my code, I see that the array length of my ByteBuffer is 6100, which means it's backed by 6100 bytes. 当我执行代码时,我发现ByteBuffer的数组长度为6100,这意味着它支持6100个字节。 These bytes are all 0s. 这些字节全为0。 However I also see that the bit set size is 0. This doesn't make sense to me.(The size should be 6100 * 8). 但是我也看到位设置大小为0。这对我来说没有意义。(大小应为6100 * 8)。

I looked at the documentation for valueOf and the description of "Returns a new bit set containing all the bits in the given byte array." 我查看了valueOf的文档和“返回包含给定字节数组中所有位的新位集”的描述。 makes sense for what I am trying to do. 对于我要做什么有意义。

The first mistake I made was to use the length of the bit set. 我犯的第一个错误是使用位集的长度。 The length is 0 which makes sense because all of the bits are 0s. 长度为0,这是有道理的,因为所有位均为0。 The size function "returns the number of bits of space actually in use by this BitSet to represent bit values." 大小函数“返回此BitSet实际使用的表示位数的空间位数”。 Shouldn't the size function return 6100 * 8 here? size函数不应该在这里返回6100 * 8吗?

Update: I just tried putting all 1s into the bytebuffer and now am getting an array length of 7000 and a bit set size of 7232 更新:我只是尝试将所有1放入字节缓冲区,现在得到的数组长度为7000,位大小为7232

BitSet is not implemented to directly maintain a buffer equal in size to the array passed in to initialize it. 未实现BitSet来直接维护一个缓冲区,该缓冲区的大小等于传递给它初始化的数组的大小。 Instead, it internally maintains just enough buffer space to track the highest bit that is on. 相反,它在内部仅保留足够的缓冲区空间来跟踪打开的最高位。 For any bit higher than that, methods like BitSet#get assume that if passed a bit index higher than what is maintained in its current buffer space, the bit must be off. 对于高于此值的任何位,如BitSet#get类的方法BitSet#get假定,如果传递的位索引高于其当前缓冲区中所保持的位索引,则该位必须为off。

There are several relevant statements in JavaDocs about the "size" or "length". JavaDocs中有一些有关“大小”或“长度”的相关声明。 From the class-level JavaDocs of BitSet : BitSet的类级JavaDocs中:

Every bit set has a current size, which is the number of bits of space currently in use by the bit set. 每个位集都有一个当前大小,即当前由位集使用的空间的位数。 Note that the size is related to the implementation of a bit set, so it may change with implementation. 请注意,大小与位集的实现有关,因此它可能随实现而变化。 The length of a bit set relates to logical length of a bit set and is defined independently of implementation. 位集合的长度与位集合的逻辑长度有关,并且独立于实现来定义。

From BitSet#length : BitSet#length

Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. 返回此BitSet的“逻辑大小”:BitSet中最高设置位的索引加1。 Returns zero if the BitSet contains no set bits. 如果BitSet不包含任何设置位,则返回零。

(Note also that in the extreme case of all bits off, it returns zero.) (还要注意,在所有位都关闭的极端情况下,它返回零。)

From BitSet#size : BitSet#size

Returns the number of bits of space actually in use by this BitSet to represent bit values. 返回此BitSet实际使用的表示位数的空间位数。 The maximum element in the set is the size - 1st element. 集合中的最大元素是size-第一个元素。

If you're interested in a deeper dive, I also suggest looking at the OpenJDK code for BitSet : 如果您对更深入的研究感兴趣,我还建议您查看BitSet的OpenJDK代码:

http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/3462d04401ba/src/share/classes/java/util/BitSet.java http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/3462d04401ba/src/share/classes/java/util/BitSet.java

Interesting parts are set , which dynamically expands buffer space as needed to set a particular bit on, and get , which is coded to return false if the requested bit index is beyond the current buffer capacity (the words member variable). set有趣的部分,该部分根据需要动态扩展缓冲区空间以设置特定位,并get ,如果所请求的位索引超出当前缓冲区容量( words成员变量),则将其编码为false

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

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