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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet( 6 );
bs1.set( 0 );
bs1.set( 1 );
bs1.set( 2 );
bs1.set( 4 );
bs2.set( 4 );
bs2.set( 6 );
bs2.set( 5 );
bs2.set( 1 );
bs2.set( 2 );
bs2.set( 3 );
System.out.println( "bs1 : " + bs1);
System.out.println( "bs2 : " + bs2);
bs2.xor(bs1);
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 1
- Bitset Class Examples Set 2
- Bitset Class Examples Set 3
Last Updated :
31 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...