Search code examples
javahuffman-codebitset

How to calculate the size of a BitSet in bytes?


I used a Java BitSet to encode a message.

My compressed file size comes out to 954kb, but when I do BitSet.cardinality(), I get around 4mb. Can you explain this?


Solution

  • BitSet.cardinality() returns the number of bits set to true in the BitSet. I think you are looking for BitSet.size(). But keep in mind it will return the number of bits, not bytes.

    Assuming after Huffman encoding you have approximately half of the bits set to true, that means your BitSet should have a size of around 4.000.000*2 = 8.000.000 bits in your BitSet which in turn makes around 1.000.000 bytes which is rather close to the 954kb you see.

    This should explain your observation.