How to calculate total number of bits in BitSet object?

拥有回忆 提交于 2019-12-24 08:59:04

问题


I want to calculate the total number of bits in a BitSet object. The method length returns the "logical size" of the BitSet: the index of the highest set bit in the BitSet plus one, while method cardinality will give the total number of bits set to 1 in the object.

I want to calculate the total number of bits including both 0s and 1s. How do I do that?


回答1:


How about BitSet.size()?.....




回答2:


The getObjectSize(Object o) method in the Instrumentation library is your guy: http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/Instrumentation.html

There are a few threads on this.

  • http://www.javaworld.com/article/2077496/testing-debugging/java-tip-130--do-you-know-your-data-size-.html
  • In Java, what is the best way to determine the size of an object?

Cheers!




回答3:


The site shows how to convert a BitSet to binary String. Then just calculate the string.length()



来源:https://stackoverflow.com/questions/11062616/how-to-calculate-total-number-of-bits-in-bitset-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!