Last Updated : 23 Jul, 2025
Methods discussed in this post: BitSet class methods. / / | | \ \ set() xor() clone() clear() length() cardinality()
We strongly recommend to refer below set 1 as a prerequisite of this.
BitSet class in Java | Set 1public void set(int bitpos) public void set(int bitpos, boolean val) Parameters: bitpos : a bit index val : a boolean value to set Return: Nothing Throws: IndexOutOfBoundsException - if the specified index is negative
public Object clone() Return: a clone of this bit set
public int cardinality() Return: the number of bits set to true in this BitSet.
// Java program explaining BitSet class methods
// set(), clone(), cardinality()
import java.util.*;
public class NewClasss
{
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet(8);
BitSet bs3 = new BitSet();
// assign values to bs1 using set()
bs1.set(0);
bs1.set(1);
bs1.set(2);
bs1.set(4);
// assign values to bs2
bs2.set(4);
bs2.set(6);
bs2.set(5);
// Here we are using .clone() method to make
// bs3 as bs1
bs3 = (BitSet) bs1.clone();
// Printing the 3 Bitsets
System.out.println("bs1 : " + bs1);
System.out.println("bs2 : " + bs2);
System.out.println("bs3 cloned from bs1 : " + bs3);
// Using .cardinality() method to print the no. of
// elements of Bitset
System.out.println("Cardinality of bs1 : " +
bs1.cardinality());
System.out.println("Cardinality of bs2 : " +
bs2.cardinality());
}
}
Output:
bs1 : {0, 1, 2, 4} bs2 : {4, 5, 6} bs3 cloned from bs1 : {0, 1, 2, 4} Cardinality of bs1 : 4 Cardinality of bs2 : 3
public void clear(int frompos,int topos) public void clear(int bitIndex) public void clear() Parameters: frompos - index of the first bit to be cleared topos - index after the last bit to be cleared bitIndex - the index of the bit to be cleared Throws: IndexOutOfBoundsException - if pos value is negative or frompos is larger than topos
public void xor(BitSet set) Parameters: set - the BitSet with which we need to perform operation
public int length() Returns: the logical size of this BitSet
// Java program explaining BitSet class methods
// xor(), length(), clear() methods
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
// assign values to bs1 using set()
bs1.set(7);
bs1.set(1);
bs1.set(2);
bs1.set(4);
bs1.set(3);
bs1.set(6);
// assign values to bs2
bs2.set(4);
bs2.set(6);
bs2.set(3);
bs2.set(9);
bs2.set(2);
// Printing the Bitsets
System.out.println("bs1 : " + bs1);
System.out.println("bs2 : " + bs2);
// use of length() method
System.out.println("use of length() : " + bs1.length());
// use of xor() to perform logical Xor operation
bs1.xor(bs2);
System.out.println("Use of xor() : " + bs1);
bs2.xor(bs1);
System.out.println("Use of xor() : " + bs2);
// clear from index 2 to index 4 in bs1
bs2.clear(1, 2);
System.out.println("bs2 after clear method : " + bs2);
// clear complete Bitset
bs1.clear();
System.out.println("bs1 after clear method : " + bs1);
}
}
Output:
bs1 : {1, 2, 3, 4, 6, 7} bs2 : {2, 3, 4, 6, 9} use of length() : 8 Use of xor() : {1, 7, 9} Use of xor() : {1, 2, 3, 4, 6, 7} bs2 after clear method : {2, 3, 4, 6, 7} bs1 after clear method : {}BitSet class methods in Java with Examples | Set 3 References : https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4