简体   繁体   中英

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. The ByteBuffer is loaded from DynamoDB(With the 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. These bytes are all 0s. However I also see that the bit set size is 0. This doesn't make sense to me.(The size should be 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." 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. The size function "returns the number of bits of space actually in use by this BitSet to represent bit values." Shouldn't the size function return 6100 * 8 here?

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

BitSet is not implemented to directly maintain a buffer equal in size to the array passed in to initialize it. 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.

There are several relevant statements in JavaDocs about the "size" or "length". From the class-level JavaDocs of BitSet :

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 :

Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. Returns zero if the BitSet contains no set bits.

(Note also that in the extreme case of all bits off, it returns zero.)

From BitSet#size :

Returns the number of bits of space actually in use by this BitSet to represent bit values. The maximum element in the set is the size - 1st element.

If you're interested in a deeper dive, I also suggest looking at the OpenJDK code for BitSet :

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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