Java Bitset Class
A Bitset class creates a special type of array to hold bit values. The BitSet array size increases as needed. This is similar to a vector of bits.
This is a traditional class, but it was completely redesigned in Java 2.
BitSet defines two constructors.
The first constructor creates a default object:
BitSet()
The second constructor allows the user to specify the initial size. All bits are initially set to 0.
BitSet(int size)
BitSet implements the methods defined in the Cloneable interface as listed in the following table:
Number | Method Description |
---|---|
1 | void and(BitSet set) <br>Performs a logical AND of this target bit set with the argument bit set. |
2 | void andNot(BitSet set) <br>Clears all bits in this BitSet whose corresponding bit is set in the specified BitSet. |
3 | int cardinality() <br>Returns the number of bits set to true in this BitSet. |
4 | void clear() <br>Sets all bits in this BitSet to false. |
5 | void clear(int index) <br>Sets the bit specified by the index to false. |
6 | void clear(int startIndex, int endIndex) <br>Sets the bits from the specified startIndex (inclusive) to the specified endIndex (exclusive) to false. |
7 | Object clone() <br>Duplicates this BitSet, creating an equal new BitSet. |
8 | boolean equals(Object bitSet) <br>Compares this object against the specified object. |
9 | void flip(int index) <br>Sets the bit at the specified index to the complement of its current value. |
10 | void flip(int startIndex, int endIndex) <br>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) <br>Returns the value of the bit with the specified index. |
12 | BitSet get(int startIndex, int endIndex) <br>Returns a new BitSet composed of bits from this BitSet from fromIndex (inclusive) to toIndex (exclusive). |
13 | int hashCode() <br>Returns the hash code value for this bit set. |
14 | boolean intersects(BitSet bitSet) <br>Returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet. |
15 | boolean isEmpty() <br>Returns true if this BitSet contains no bits that are set to true. |
16 | int length() <br>Returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus 1. |
17 | int nextClearBit(int startIndex) <br>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) <br>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) <br>Performs a logical OR of this bit set with the bit set argument. |
20 | void set(int index) <br>Sets the bit at the specified index to true. |
21 | void set(int index, boolean v) <br>Sets the bit at the specified index to the specified value. |
22 | void set(int startIndex, int endIndex) <br>Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true. |
23 | void set(int startIndex, int endIndex, boolean v) <br>Sets the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value. |
24 | int size() <br>Returns the number of bits of space actually in use by this BitSet to represent bit values. |
25 | String toString() <br>Returns a string representation of this bit set. |
26 | void xor(BitSet bitSet) <br>Performs a logical XOR of this bit set with 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);
// set some bits
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("\nInitial pattern in bits2: ");
System.out.println(bits2);
// AND bits
bits2.and(bits1);
System.out.println("\nbits2 AND bits1: ");
System.out.println(bits2);
// OR bits
bits2.or(bits1);
System.out.println("\nbits2 OR bits1: ");
System.out.println(bits2);
// XOR bits
bits2.xor(bits1);
System.out.println("\nbits2 XOR bits1: ");
System.out.println(bits2);
}
}
The above example compiles and runs with the following result:
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:
{}