Java Bitset Class

Java 数据结构Java Data Structures


A Bitset class creates a special type of array to hold bit values. The array size of BitSet will increase 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 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:

No. Method Description
1 void and(BitSet set)
Performs a logical AND operation on this target bit set and the argument 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 toIndex (exclusive) to false.
7 Object clone( )
Clones this BitSet to produce a new BitSet that is equal to it.
8 boolean equals(Object bitSet)
Compares this object with 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 bit value at the specified index.
12 BitSet get(int startIndex, int endIndex)
Returns a new BitSet composed of the bits in this BitSet from fromIndex (inclusive) to toIndex (exclusive).
13 int hashCode( )
Returns the hash code value of this bit set.
14 boolean intersects(BitSet bitSet)
Returns true if there is a bit set to true in the specified BitSet that is 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 1.
17 int nextClearBit(int startIndex)
Returns the index of the first bit set to false that occurs at or after the specified starting index.
18 int nextSetBit(int startIndex)
Returns the index of the first bit set to true that occurs at or after the specified starting index.
19 void or(BitSet bitSet)
Performs a logical OR operation on 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 used by this BitSet when representing bit values.
25 String toString( )
Returns a string representation of this bit set.
26 void xor(BitSet bitSet)
Performs a logical XOR operation on 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); // 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); } }

Compiling and running the above example produces 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:
{}

Java 数据结构Java Data Structures

Other Extensions