简体   繁体   中英

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?

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.

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