java – Why is BitSet implemented on a long array


For some reason, nowhere on the Internet is the choice of long as a BitSet store explained. For example, why can't you use short instead. Let us fill the structure with N ( N< 64 ) elements, then it will turn out that in comparison with short , the memory overhead will be larger.


I have no idea what Bitset is and I'm not familiar with java. But if you make some kind of base class for working with bits and not make it with a variable size of storage elements, I would stop at choosing the maximum size type that fits into the processor register for this architecture. From the processor's point of view, an operation with short and long takes the same execution time. In addition, the operation with data in RAM that is not aligned to the width of the data bus usually takes longer. In this regard, they try to align the data to the width of the bus. Trying to flatten short will result in wasted space, which will bring the memory savings to 0

Modern intel architecture uses 64 bit registers and needs the same data alignment in memory to run fast

Scroll to Top