|
(1) bitset class
The size can be dynamically changed. values are set to true or false. Indicates a group of Boolean flag.
This class implements a bitvector that increases on demand. Each component of a bit set has a Boolean value. Use a non-negative integer to index bitset bits. You can test, set, or clear each indexed bit. You can use one bitset to modify the content of another bitset through logic and, logic or logical XOR or operation.
By default, the initial values of all bits in the set are false.
Each bit set has a current size, that is, the number of digits of the space currently used by the bit set. Note that this size is related to the implementation of the bit set, so it may be changed as it is implemented. The length of a bit set is related to the logical length of a bit set and is defined regardless of the implementation.
Unless otherwise specified, passing the null parameter to any method in the bitset will cause nullpointerexception. In the absence of external synchronization, it is unsafe to operate on one bitset by multiple threads. |
|
(2) constructor: bitset () or bitset (INT nbits)
(3) Some Methods
Public void set (int pos): Set the POs field to true.
Public void set (INT bitindex, Boolean value) sets the bit at the specified index to the specified value.
Public void clear (int pos): Set the POs field to false.
Public void clear (): Set all bits in this bitset to false.
Public int cardinality () returns the number of digits set to true in this bitset.
Public Boolean get (int pos): returns the POs bitwise value.
Public void and (bitset other): Other performs and operates on this bitset, and the result is used as the new value of this bitset.
Public void or (bitset other): The other performs or operations on the bitset, and the result is the new value of the bitset.
Public void XOR (bitset other): Other performs an exception or operation with this bitset, and the result is used as the new value of this bitset.
Public void Andnot (bitset set) clears all bits in this bitset. Set-is used to block the bitset of this bitset.
Public int size (): returns the bitset that represents the number of actually used space bits.
Public int length () returns the "logical size" of this bitset: Add 1 to the index with the highest set bit in the bitset.
Public int hashcode (): returns the hash code of the Set, which is related to the bitwise value in the set.
Public Boolean equals (object other): If the characters in other are the same as those in the Set, true is returned.
Public object clone () clone this bitset to generate a new bitset equal to it.
Public String tostring () returns the string representation of this set.
Example 1: Specify the characters used in a string
import java.util.BitSet;public class WhichChars{ private BitSet used = new BitSet(); public WhichChars(String str){ for(int i=0;i< str.length();i++) used.set(str.charAt(i)); // set bit for char } public String toString(){ String desc="["; int size=used.size(); for(int i=0;i< size;i++){ if(used.get(i)) desc+=(char)i; } return desc+"]"; } public static void main(String args[]){ WhichChars w=new WhichChars("How do you do"); System.out.println(w); } }
Run:
C: \ work> JAVA whichchars
[Hdouwy]
Example 2: Evaluate the prime number using the 'distinct' Method
import java.util.*;public class BitSetTest{ public static void main(String[] args){ BitSet sieve=new BitSet(1024); int size=sieve.size(); for(int i=2;i< size;i++) sieve.set(i); int finalBit=(int)Math.sqrt(sieve.size()); for(int i=2;i< finalBit;i++) if(sieve.get(i)) for(int j=2*i;j< size;j+=i) sieve.clear(j); int counter=0; for(int i=1;i< size;i++){ if(sieve.get(i)){ System.out.printf("%5d",i); if(++counter%15==0) System.out.println(); } } }}C:\work>java BitSetTest 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021
Example 3: simple use
import java.util.BitSet;public class BitOHoney { public static void main(String args[]) { String names[] = { "Java", "Source", "and", "Support"}; BitSet bits = new BitSet(); for (int i = 0, n = names.length; i < n; i++) { if ((names[i].length() % 2) == 0) { bits.set(i); } } System.out.println(bits); System.out.println("Size : " + bits.size()); System.out.println("Length: " + bits.length()); for (int i = 0, n = names.length; i < n; i++) { if (!bits.get(i)) { System.out.println(names[i] + " is odd"); } } BitSet bites = new BitSet(); bites.set(0); bites.set(1); bites.set(2); bites.set(3); bites.andNot(bits); System.out.println(bites); }}
Run:
C: \ work> JAVA bitohoney
{0, 1}
Size: 64
Length: 2
And is odd
Support is odd
{2, 3}