The BitSet class

The BitSet class creates dynamic array which can holds bit values and can increase its size when needed. So its like vector of bit.

The BitSet class

The BitSet class creates dynamic array which can holds bit values and can increase its size when needed. So its like vector of bit.

The BitSet class

The BitSet class

The BitSet class creates dynamic array which can holds bit values and can increase its size when needed. So its like vector of bit.

It supports two type of constructor :

(1) The first constructor creates a default object. Syntax is given below : BitSet( )

(2) The second constructor gives freedom to user to set initial size. Syntax is given below :

BitSet(int size)

Each element of BitSet holds boolean value. These elements are indexed by nonnegative integers. One BitSet may be used to change the other BitSet content logical AND, logical inclusive OR, and logical exclusive OR operations.

For complete list of methods of BitSet  Class, Click here

EXAMPLE

import java.util.*;

public class BitSetExample {
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);
}

}

OUTPUT

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:
{}

Download Source Code