Java BitSet | XOR

BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.


public void xor(BitSet set);

The method performs a logical XOR, such that in the BitSet, a bit is set if and only if the bit initially has the value true, and the corresponding bit in the argument has the value false, and vice-versa.


Input : 
set1 : {1, 2, 4}
set2 : {2, 3, 4}

Output :
After performing st1.xor(set2)
set2 : {1, 3}






// Java program illustrating Bitset Class Function.
import java.util.*;
public class GFG {
    public static void main(String[] args)
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet(6);
        /* set is BitSet class method
        expalined in next articles */
        // assign values to bs2
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
        // Performing logical AND
        // on bs2 set with bs1
        // bs2 set after Performing AND
        System.out.println("After Performing XOR :");



bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}
After Performing XOR :
{0, 3, 5, 6}

Related Articles :

  1. Bitset Class Examples Set 1
  2. Bitset Class Examples Set 2
  3. Bitset Class Examples Set 3

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.