YouTip LogoYouTip

Java Bitset Class

[![Image 1: Java Data Structures](#)Java Data Structures](#) * * * A BitSet class creates a special type of array to hold bit values. The BitSet array size grows as needed. This is similar to a vector of bits. This is a legacy class, but it was completely redesigned in Java 2. BitSet defines two constructors. The first constructor creates a default object: BitSet() The second method allows the user to specify an initial size. All bits are initialized to 0. BitSet(int size) The methods defined in the Cloneable interface implemented in BitSet are listed in the following table: | Serial Number | Method Description | | --- | --- | | 1 | void and(BitSet set) Performs a logical AND operation between this target bit set and the parameter bit set. | | 2 | void andNot(BitSet set) Clears all bits in this BitSet whose corresponding bits are set in the specified BitSet. | | 3 | int cardinality( ) Returns the number of bits set to true in this BitSet. | | 4 | void clear( ) Sets all bits in this BitSet to false. | | 5 | void clear(int index) Sets the bit at the specified index to false. | | 6 | void clear(int startIndex, int endIndex) Sets the bits from the specified startIndex (inclusive) to the specified endIndex (exclusive) to false. | | 7 | Object clone( ) Copies this BitSet, producing a new BitSet that is equal to it. | | 8 | boolean equals(Object bitSet) Compares this object to the specified object. | | 9 | void flip(int index) Sets the bit at the specified index to the complement of its current value. | | 10 | void flip(int startIndex, int endIndex) Sets each bit from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the complement of its current value. | | 11 | boolean get(int index) Returns the value of the bit at the specified index. | | 12 | BitSet get(int startIndex, int endIndex) Returns a new BitSet composed of bits from this BitSet from fromIndex (inclusive) to toIndex (exclusive). | | 13 | int hashCode( ) Returns the hash code value for this bit set. | | 14 | boolean intersects(BitSet bitSet) Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet. | | 15 | boolean isEmpty( ) Returns true if this BitSet contains no bits set to true. | | 16 | int length( ) Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. | | 17 | int nextClearBit(int startIndex) Returns the index of the first bit that is set to false that occurs on or after the specified starting index. | | 18 | int nextSetBit(int startIndex) Returns the index of the first bit that is set to true that occurs on or after the specified starting index. | | 19 | void or(BitSet bitSet) Performs a logical OR operation between this bit set and the bit set argument. | | 20 | void set(int index) Sets the bit at the specified index to true. | | 21 | void set(int index, boolean v) Sets the bit at the specified index to the specified value. | | 22 | void set(int startIndex, int endIndex) Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true. | | 23 | void set(int startIndex, int endIndex, boolean v) Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value. | | 24 | int size( ) Returns the number of bits of space actually in use by this BitSet to represent bit values. | | 25 | String toString( ) Returns a string representation of this bit set. | | 26 | void xor(BitSet bitSet) Performs a logical XOR operation between this bit set and the bit set argument. | ### Example The following program illustrates several methods supported by this data structure: ## Example import java.util.BitSet; public class BitSetDemo{public static void main(String args[]){BitSet bits1 = new BitSet(16); BitSet bits2 = new BitSet(16); for(int i=0; i<16; i++){if((i%2) == 0)bits1.set(i); if((i%5) != 0)bits2.set(i); }System.out.println("Initial pattern in bits1: "); System.out.println(bits1); System.out.println("n Initial pattern in bits2: "); System.out.println(bits2); bits2.and(bits1); System.out.println("n bits2 AND bits1: "); System.out.println(bits2); bits2.or(bits1); System.out.println("n bits2 OR bits1: "); System.out.println(bits2); bits2.xor(bits1); System.out.println("n bits2 XOR bits1: "); System.out.println(bits2); }} The compilation and execution results of the above example are as follows: Initial pattern in bits1:{0, 2, 4, 6, 8, 10, 12, 14}Initial pattern in bits2:{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14} bits2 AND bits1:{2, 4, 6, 8, 12, 14} bits2 OR bits1:{0, 2, 4, 6, 8, 10, 12, 14} bits2 XOR bits1:{} * * Java Data Structures](#)
← Java Vector ClassJava Enumeration Interface β†’