Java Utility Library

Java BitSet - xor() Method



The java.util.BitSet.xor() method is used to perform a logical XOR operation on the given BitSet with the specified BitSet argument. The given BitSet is modified so that a bit in it has the value true if one of the following statements holds:

  • The bit initially has the value true, and the corresponding bit in the argument has the value false.
  • The bit initially has the value false, and the corresponding bit in the argument has the value true.

Syntax

public void xor(BitSet set)

Parameters

set Specify a BitSet.

Return Value

void type.

Exception

NA.

Example:

In the example below, the java.util.BitSet.xor() method is used to perform XOR operation on a given BitSet called BSet1 using the specified BitSet called BSet2.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating two BitSet
    BitSet BSet1 = new BitSet();
    BitSet BSet2 = new BitSet();

    //populating BSet1
    BSet1.set(2);
    BSet1.set(4);
    BSet1.set(6);
    BSet1.set(8);
    BSet1.set(10);

    //populating BSet2
    BSet2.set(4);
    BSet2.set(8);
    BSet2.set(12);   

    //printing BSet1
    System.out.println("Before XOR, BSet1 contains: " + BSet1);

    //performing XOR operation on 
    //BSet1 using BSet2
    BSet1.xor(BSet2);

    //printing BSet1
    System.out.println("After XOR, BSet1 contains: " + BSet1);
  }
}

The output of the above code will be:

Before XOR, BSet1 contains: {2, 4, 6, 8, 10}
After XOR, BSet1 contains: {2, 6, 10, 12}

❮ Java.util - BitSet