Latest web development tutorials

Java Bitset class

A Bitset class creates a special type of array to hold the bit value. BitSet with the size of the array will need to increase. This bit vector (vector of bits) compare like with like.

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 the initial size. All bits are initialized to 0.


BitSet(int size)

BitSet method implemented Cloneable interface defined in the table below as follows:

No. Method Description
1 void and (BitSet bitSet)
This target bit set with the argument bit set and perform logic operations.
2 void andNot (BitSet bitSet)
Clear all of the bits in this BitSet whose corresponding bit is set in the specified BitSet.
3 int cardinality ()
Returns the number of bits in this BitSet set to true.
4 void clear ()
All of the bits in this BitSet to false.
5 void clear (int index)
The bit specified by the index to false.
6 void clear (int startIndex, int endIndex)
The specified fromIndex (inclusive) to the specified toIndex (not included) bit is set in the range of false.
7 Object clone ()
Copy this BitSet, generate new BitSet that is equal.
8 boolean equals (Object bitSet)
This object against the specified object to be compared.
9 void flip (int index)
The bit at the specified index to its current value of complement.
10 void flip (int startIndex, int endIndex)
The specified fromIndex (inclusive) to the specified toIndex (not included) for each bit within the scope of its current value of complement.
11 boolean get (int index)
Returns the bit value at the specified index.
12 BitSet get (int startIndex, int endIndex)
Returns a new BitSet, this BitSet composed from fromIndex (inclusive) to toIndex (not included) position within the range.
13 int hashCode ()
Returns the hash code value for this bit set.
14 boolean intersects (BitSet bitSet)
If the specified BitSet has any bits set to true, and in this BitSet will also be set to true, then return ture.
15 boolean isEmpty ()
If this BitSet contains no bits set to true, then return ture.
16 int length ()
Returns of this BitSet "logical size": BitSet index of the highest set bit plus one.
17 int nextClearBit (int startIndex)
Returns the first index is set to false bit, which occurs on or after the specified index starting index.
18 int nextSetBit (int startIndex)
Returns the first bit is set to true index, which occurs on or after the specified index starting index.
19 void or (BitSet bitSet)
This bit set with the bit set parameters or to perform logical operations.
20 void set (int index)
The bit at the specified index is set to true.
twenty one void set (int index, boolean v)
The bit at the specified index to the specified value.
twenty two void set (int startIndex, int endIndex)
The specified fromIndex (inclusive) to the specified toIndex (not included) bit is set in the range of true.
twenty three void set (int startIndex, int endIndex, boolean v)
The specified fromIndex (inclusive) to the specified toIndex (not included) bit is set within the range of the specified value.
twenty four int size ()
Returns BitSet represents the actual use of space when the bit values ​​of bits.
25 String toString ()
Returns a string representation of this bit set.
26 void xor (BitSet bitSet)
This bit set with the bit set parameters to perform a logical XOR operation.

Examples

The following program illustrates this data structure supports several methods:

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 examples compiled results 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:
{}