The
Bitset is actually a vector composed of "bits". If you want to efficiently save a large number of "open-close" information, you should use Bitset. It only makes sense in terms of size; If you want efficient access, it's slower than using some of the intrinsic types of arrays.
Additionally, the minimum length of a bitset is the length of a long Integer (long): 64 bits. This means that if we are prepared to save smaller data, such as 8-bit data, then Bitset is wasted. So it's best to create your own class and use it to hold your own logo bits.
in an ordinary vector, as we add more and more elements, the collection expands itself. In a way, Bitset is no exception. That is, it sometimes expands itself, sometimes otherwise. and the 1.0 version of Java seems to have done the worst in this regard, and its bitset performance is quite passable (Java1.1 has corrected the problem). The following example shows how Bitset works, while demonstrating the 1.0 version of the error:
: Bits.java//demonstration of bitset import java.util.*;
public class Bits {public static void main (string[] args) {Random rand = new Random ();
Take the LSB of Nextint (): Byte BT = (byte) rand.nextint ();
Bitset BB = new Bitset ();
for (int i = 7; I >=0; i--) if (((1 << i) & BT)!= 0) Bb.set (i);
else Bb.clear (i);
System.out.println ("Byte value:" + BT);
Printbitset (BB);
Short St. = (short) rand.nextint ();
Bitset bs = new Bitset ();
for (int i = i >=0; i--) if (((1 << i) & St)!= 0) Bs.set (i);
else Bs.clear (i);
SYSTEM.OUT.PRINTLN ("Short Value:" + st);
Printbitset (BS);
int it = Rand.nextint ();
Bitset bi = new Bitset ();
for (int i = i >=0; i--) if (((1 << i) & it)!= 0) Bi.set (i);
else Bi.clear (i);
SYSTEM.OUT.PRINTLN ("int value:" + it);
Printbitset (BI); Test bitsets >= BiTs:bitset b127 = new Bitset ();
B127.set (127);
System.out.println ("Set bit 127:" + b127);
Bitset b255 = new Bitset (65);
B255.set (255);
System.out.println ("Set bit 255:" + b255);
Bitset b1023 = new Bitset (512);
Without the following, an exception are thrown//in the Java 1.0 implementation of Bitset://B1023.set (1023);
B1023.set (1024);
System.out.println ("Set bit 1023:" + b1023);
} static void Printbitset (Bitset b) {System.out.println ("bits:" + b);
String bbits = new string (); for (int j = 0; J < B.size (); j + +) Bbits + = (B.get (j)?
"1": "0");
System.out.println ("bit pattern:" + bbits); }
} ///:~
A random number generator is used to create a random byte, short, and Int. Each will be converted into a corresponding bit model within the Bitset. At this point everything is normal, because the Bitset is 64 bits, so they do not cause the final size of the increase. But in Java 1.0, once the Bitset is greater than 64 bits, there are some confusing behaviors. If we set a bit that is only 1 larger than the Bitset current allocated storage space, it can expand normally. But once you try to set a bit in a higher position without first touching the border, you get an annoying violation. This is precisely because Bitset is not properly extended in Java 1.0. This example creates a 512-bit bitset. The builder allocates storage space twice times the number of digits. So if you set a bit 1024 or higher and don't set bit 1023 first, you get a violation in Java 1.0. Fortunately, the problem has been corrected in Java 1.1. So if you're writing code for Java 1.0, try to avoid using Bitset.