简体   繁体   English

Java Bitset类与Byte数组的比较-Byte数组优于Bitset类的优点

[英]Comparision of Java Bitset class with Byte array- Advantages of Byte array over Bitset class

The Java Bitset class is dynamic and its size changes accordinly with the bits ON in it. Java Bitset类是动态的,其大小与其中的ON一致。 But Byte array can hold specific number of bits and it is easy to be used with the SQL server for copying and retrieving data from database. 但是Byte数组可以保存特定数量的位,因此很容易与SQL Server一起使用,以从数据库中复制和检索数据。 Is this true? 这是真的?

Can Bitset class be used for very large bit calculations (logical operations OR, AND). Bitset类可以用于非常大的位计算(逻辑运算OR,AND)。 Or byte array has the edge over it as with byte array we can deal with specific parts of the bits and modify them accordingly? 还是字节数组具有优势,就像字节数组一样,我们可以处理位的特定部分并进行相应的修改?

If there is a standard class that provides the functionality you need, you should use that class in preference to writing your own code. 如果有一个提供所需功能的标准类,则应优先使用该类来编写自己的代码。 Because the writers of the library are probably (no offence meant) better programmers than you and the code has already been tested and debugged. 因为库的编写者可能(不是冒犯)比您更好的程序员,并且代码已经过测试和调试。 Only if you have concrete facts to indicate that the standard class has insufficient performance should you consider writing your own... and even then your efforst are unliklely to be successful. 仅当您有具体的事实表明标准类的性能不足时,才应考虑编写自己的类……即使如此,您的毅力也很难取得成功。

byte[] is a magnitude larger than a bitset for the same amount of data (ie. 8 times as much space is allocated), because a byte = 8 bits. 对于相同数量的数据(即分配的空间是8倍),byte []的大小要大于位集,因为byte = 8位。 What also works is using an int/long to represent a bitset, which is what I used to represent large enum sets in a database. 也可以使用int / long表示位集,这就是我用来表示数据库中的大型枚举集的方式。 Why would you shy away from a BitSet if it is made especially for the purpose of storing bits... 如果专门为了存储位而制作了BitSet,为什么还要避开它呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM