Search code examples
javabitset

How BitSet admit long array with length 6?


BitSet internally uses long array of size 6.

But it can contain 2^31-1 bits.

long = 64 bit

6 longs = 64 * 6. But is much less than 2^31-1. Please explain this trick.


Solution

  • The internal array which stores the values expands if necessary, just like an ArrayList would do for objects. Your statement is therefore false.

    There is a hard limit of 2^31-1 (bits) for the size, but that's only because the operations (set, flip, etc.) take the index parameter as an int. Which is probably big enough for most use cases.