Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to java.util.BitSet for small sizes?

java.util.BitSet is backed by a long[] so the minimum size is 64 bits. I require to cache lots (~2M) of objects that each require a BitSet of size 23. Is there an alternative to BitSet that is more space efficient for small sizes? For example is there a BitSet type data structure that is backed by a byte[] instead of a long[]? This would allow me to store my 23 bits in 3 bytes instead of 8.

like image 874
mR_fr0g Avatar asked Jan 31 '26 07:01

mR_fr0g


1 Answers

The java.util.BitSet class is designed for larger bit sets. When you need bit sets of size 23, even a bit set based ob 3 bytes would use too much memory, because an array of any size uses an additional reference for the array itself, which is most likely four to eight bytes.

The most economical solution in terms of memory is using ints instead of bit sets, and writing your own implementations of the bit set operations that you need. Since operations on bit sets are for the most part copied from bitwise operations, you should have no problem implementing them:

boolean get(int mySet, int index) {
    return (mySet & (1<<index)) != 0;
}
int set(int mySet, int index) {
    return mySet | (1<<index);
}
int clear(int mySet, int index) {
    return mySet & (1<<index);
}

...and so on.

like image 79
Sergey Kalinichenko Avatar answered Feb 01 '26 23:02

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!