BitSet in Java |

## BitSet in Java:

All About Class BitSet in Java implements a vector or set of bits that grows as needed. It defines an arbitrarily large set of bits. Each component of the BitSet has a boolean value. The bits of a BitSet represents non-negative integers. Individual bits can be examined, set or cleared. By default, all bits in the BitSet initially have the value false.

##### All About Class BitSet in Java

Every bit set has current size, which is the number of bits currently in the BitSet. This class has several methods to set, clear and query individual bits in the set. This class also allows bitwise boolean arithmetic on the bits in BitSet objects.

The class BitSetÂ is defined as:

```
public final class java.util.BitSet extends
java.lang.Object implements
java.lang.Cloneable,
java.io.Serializable{
//Constructors:
public BitSet()//Creates an empty BitSet whose bits are initially false.
// Defautl capacity-64 bits
public BitSet (int nBits)////Creates an empty BitSet with size nBits the specific number
//of bits.whose bits are initially false.
//Methods:
public void and(Bitset set);
//This method returns a BitSet by calculating logical AND of this BitSet.
//For each bit in this BitSet is set to true if the the bit is already true.
//If the size of the set is greater than the size of this BitSet,the extra bits
//in the set are ignored. Otherwise , if the size of set is less than the size
//of this BitSet,the extra bits in this BitSet are set to false.
public void clear(int bits);
//This method sets the bit at the specified index to false.If bit size is greater
//than or equal to the number of bits in the BitSet,the size of the BitSet is increased
//so that it contains bit values.All of the additional bits are set to false.
public void or(Bitset set);
//this method returns a BitSet after calculating the logical OR of this BitSet.Each
//bit in the resultant BitSet is set to true if the bit is already true in this BitSet.
//If the size of set is greater than the size of this BitSet,the BitSet is first
//increased in size to accommodate the additional bits.All extra bits are initially set
//to false.
public void set(int bits);
//this method sets the given index to true to this BitSet.If the size of set is greater
//than the size of this BitSet,the BitSet is first increased in size to accommodate the
//additional bits.All extra bits are initially set to false.
public void xor(Bitset set);
//This method returns a BitSet after calculating logical XOR(exclusive OR) of this BitSet.
//Each bit is this BitSet,the value is set to true only if the bit is already true in this
//BitSet and the corresponding bit in set is false or if the bit is false in this BitSet
//the corresponding bit in set is true.
//If the size of set is greater
//than the size of this BitSet,the BitSet is first increased in size to accommodate the
//additional bits.All extra bits are initially set to false.
public Object clone();//overrides Object.clone() method and creates a copy of this
//BitSet.and returns it.The resultant BitSet has the same size as this BitSet and
//the same bits are set to true.
public boolean equals(Object obj);//overrides Object.equals() method and returns
//true if the obj is an instance of BitSet and contains the same bit values.This
//method compares each bit of this BitSet with the corresponding bits of the
//object.If any bit does not match the method returns false.If the size of this
//BitSet is different than object,the extra bits in either object or this
//BitSet must be false.
public boolean get(int bit);//This method returns the value of the given bit.If
//the bit is greater than or equal to the number of bits in the BitSet,the method
//returns false.
public int hashCode();//overrides Object.hashCode() method and returns the hashCode
//for this object.
public int size();//This method returns the size of this BitSet.It returns the number
//of bits currently in the BitSet.
public String toString();//overrides Object.toString() method and returns the String
//representation of this BitSet.The String lists the index as of all the bits in the
//BitSet that are true.
```

}

]Apart from these BitSet class also contains the Inherited methods from Object class:

They are as follows:

##### Class BitSet in Java

- finalize()
- getClass()
- notify()
- notifyAll()
- wait()
- wait(long timeput)
- wait(long timeout,int nanos)

We can use BitSet class as an extermly compact array of boolean values but reading and writting those values are slower than normal array access.