The perfect place for easy learning...

Java Programming

×

Topics List


BitSet class in java





The BitSet is a built-in class in java used to create a dynamic array of bits represented by boolean values. The BitSet class is available inside the java.util package.

The BitSet array can increase in size as needed. This feature makes the BitSet similar to a Vector of bits.

🔔 The bit values can be accessed by non-negative integers as an index.

🔔 The size of the array is flexible and can grow to accommodate additional bit as needed.

🔔 The default value of the BitSet is boolean false with a representation as 0 (off).

🔔 BitSet uses 1 bit of memory per each boolean value.

The BitSet class in java has the following constructor.

S. No. Constructor with Description
1 BitSet( )

It creates a default BitSet object.

2 BitSet(int noOfBits)

It creates a BitSet object with number of bits that it can hold. All bits are initialized to zero.

The BitSet class in java has the following methods.

S.No. Methods with Description
1 void and(BitSet bitSet)

It performs AND operation on the contents of the invoking BitSet object with those specified by bitSet.

2 void andNot(BitSet bitSet)

For each 1 bit in bitSet, the corresponding bit in the invoking BitSet is cleared.

3 void flip(int index)

It reverses the bit at specified index.

4 void flip(int startIndex, int endIndex)

It reverses all the bits from specified startIndex to endIndex.

5 void or(BitSet bitSet)

It performs OR operation on the contents of the invoking BitSet object with those specified by bitSet.

6 void xor(BitSet bitSet)

It performs XOR operation on the contents of the invoking BitSet object with those specified by bitSet.

7 int cardinality( )

It returns the number of bits set to true in the invoking BitSet.

8 void clear( )

It sets all the bits to zeros of the invoking BitSet.

9 void clear(int index)

It set the bit specified by given index to zero.

10 void clear(int startIndex, int endIndex)

It sets all the bits from specified startIndex to endIndex to zero.

11 Object clone( )

It duplicates the invoking BitSet object.

12 boolean equals(Object bitSet)

It retruns true if both invoking and argumented BitSets are equal, otherwise returns false.

13 boolean get(int index)

It retruns the present state of the bit at given index in the invoking BitSet.

14 BitSet get(int startIndex, int endIndex)

It returns a BitSet object that consists all the bits from startIndex to endIndex.

15 int hashCode( )

It returns the hash code of the invoking BitSet.

16 boolean intersects(BitSet bitSet)

It returns true if at least one pair of corresponding bits within the invoking object and bitSet are 1.

17 boolean isEmpty( )

It returns true if all bits in the invoking object are zero, otherwise returns false.

17 int length( )

It returns the total number of bits in the invoking BitSet.

18 int nextClearBit(int startIndex)

It returns the index of the next cleared bit, (that is, the next zero bit), starting from the index specified by startIndex.

19 int nextSetBit(int startIndex)

It returns the index of the next set bit (that is, the next 1 bit), starting from the index specified by startIndex. If no bit is set, -1 is returned.

20 void set(int index)

It sets the bit specified by index.

21 void set(int index, boolean value)

It sets the bit specified by index to the value passed in value.

22 void set(int startIndex, int endIndex)

It sets all the bits from startIndex to endIndex.

23 void set(int startIndex, int endIndex, boolean value)

It sets all the bits to specified value from startIndex to endIndex.

24 int size( )

It returns the total number of bits in the invoking BitSet.

25 String toString( )

It returns the string equivalent of the invoking BitSet object.

Let's consider an example program to illustrate methods of BitSet class.

Example
import java.util.*;

public class BitSetClassExample {	
	
	public static void main(String[] args) {

		BitSet bSet_1 = new BitSet();
		BitSet bSet_2 = new BitSet(16);
		
		bSet_1.set(10);
		bSet_1.set(5);
		bSet_1.set(0);
		bSet_1.set(7);
		bSet_1.set(20);
		
		bSet_2.set(1);
		bSet_2.set(15);
		bSet_2.set(20);
		bSet_2.set(77);
		bSet_2.set(50);
		
		System.out.println("BitSet_1 => " + bSet_1);
		System.out.println("BitSet_2 => " + bSet_2);
		
		bSet_1.and(bSet_2);		
		System.out.println("BitSet_1 after and with bSet_2 => " + bSet_1);
		
		bSet_1.andNot(bSet_2);	
		System.out.println("BitSet_1 after andNot with bSet_2 => " + bSet_1);
		
		System.out.println("Length of the bSet_2 => " + bSet_2.length());
		System.out.println("Size of the bSet_2 => " + bSet_2.size());
		
		System.out.println("Bit at index 2 in bSet_2 => " + bSet_2.get(2));
		
		bSet_2.set(2);
		System.out.println("Bit at index 2 after set in bSet_2 => " + bSet_2.get(2));
	}
}

When we execute the above code, it produce the following output.

BitSet class in java