Java BitSet | XOR Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Syntax public void xor(BitSet set); Explanation: 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. Examples: Input : set1 : {1, 2, 4} set2 : {2, 3, 4} Output : After performing st1.xor(set2) set2 : {1, 3} Program: Java // 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 explained in next articles */ 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); bs2.set(1); bs2.set(2); bs2.set(3); // Printing the 2 Bitsets System.out.println("bs1 : " + bs1); System.out.println("bs2 : " + bs2); // Performing logical AND // on bs2 set with bs1 bs2.xor(bs1); // bs2 set after Performing AND System.out.println("After Performing XOR :"); System.out.println(bs2); } } Output: bs1 : {0, 1, 2, 4} bs2 : {1, 2, 3, 4, 5, 6} After Performing XOR : {0, 3, 5, 6} Related Articles : Bitset Class Examples Set 1Bitset Class Examples Set 2Bitset Class Examples Set 3 Comment More infoAdvertise with us Next Article Java BitSet | and B barykrg Follow Improve Article Tags : Misc Java Java - util package Bitwise-XOR Java-Functions Java-BitSet +2 More Practice Tags : JavaMisc Similar Reads Java BitSet | or() Prerequisite : Java BitSet | Set 1BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. It performs a logical OR of the caller BitSet with the called BitSet. This BitSet gets set only when it is true and the BitSet argument has the value true. 2 min read Java BitSet | or() Prerequisite : Java BitSet | Set 1BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. It performs a logical OR of the caller BitSet with the called BitSet. This BitSet gets set only when it is true and the BitSet argument has the value true. 2 min read Java BitSet | and Prerequisite : Java BitSet | Set 1BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if i 2 min read Java BitSet | and Prerequisite : Java BitSet | Set 1BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.Performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if i 2 min read Bitwise Operators in Java In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T 6 min read Bitwise Operators in Java In Java, Operators are special symbols that perform specific operations on one or more than one operands. They build the foundation for any type of calculation or logic in programming.There are so many operators in Java, among all, bitwise operators are used to perform operations at the bit level. T 6 min read Like