如何以字节为单位计算 BitSet 的大小?
How to calculate the size of a BitSet in bytes?
我使用 Java BitSet
对消息进行编码。
我的压缩文件大小为 954kb,但当我压缩后 BitSet.cardinality()
,我得到大约 4mb。你能解释一下吗?
BitSet.cardinality()
returns the number of bits set to true
in the BitSet
. I think you are looking for BitSet.size()
。但请记住,它将 return 位 的数量,而不是 字节 .
假设在霍夫曼编码后你有大约一半的位设置为 true
,这意味着你的 BitSet
应该有大约 4.000.000*2 = 8.000.000 位的大小BitSet
这反过来又产生了大约 1.000.000 字节,这非常接近您看到的 954kb。
这应该可以解释您的观察结果。
我使用 Java BitSet
对消息进行编码。
我的压缩文件大小为 954kb,但当我压缩后 BitSet.cardinality()
,我得到大约 4mb。你能解释一下吗?
BitSet.cardinality()
returns the number of bits set to true
in the BitSet
. I think you are looking for BitSet.size()
。但请记住,它将 return 位 的数量,而不是 字节 .
假设在霍夫曼编码后你有大约一半的位设置为 true
,这意味着你的 BitSet
应该有大约 4.000.000*2 = 8.000.000 位的大小BitSet
这反过来又产生了大约 1.000.000 字节,这非常接近您看到的 954kb。
这应该可以解释您的观察结果。