Bitset class also allows some of the logical operations on two Bitsets. The logical operations supported are : and, andNot, or, Xor. These are discussed in this article.
1. and(Bitset set) : This method performs a logical AND of this target bit set with the argument bit set and returns the values of 1st bitset also present in second bitset.
Declaration : public void and(BitSet set) Parameters : set : a bit set Return Value : This method does not return a value.
// Java code to demonstrate the working // of and(Bitset set) in Bitset import java.util.*;
public class BitSetAnd {
public static void main(String[] args)
{
// Declaring 2 bitsets
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
// adding the values to bset1
// using set()
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
// adding the values to bset2
// using set()
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
// printing the initial sets
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
// perform "and" operation between two bitsets
// using and()
bset1.and(bset2);
// printing the new bset1
System.out.println( "The resultant bset1 after and operation is : " + bset1);
}
} |
Output :
The elements of Bitset 1 are : {0, 1, 2, 3} The elements of Bitset 2 are : {0, 2, 4, 6} The resultant bset1 after and operation is : {0, 2}
2. andNot(BitSet set) : This method performs a logical NAND and returns elements of 1st Bitset that are not present in argument Bitset.
Declaration : public void andNot(BitSet set) Parameters : set: the BitSet with which to mask this BitSet. Return Value : This method does not return a value.
// Java code to demonstrate the working // of andNot(Bitset set) in Bitset import java.util.*;
public class BitSetNotAnd {
public static void main(String[] args)
{
// Declaring 2 bitsets
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
// adding the values to bset1
// using set()
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
// adding the values to bset2
// using set()
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
// printing the initial sets
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
// perform "not-and" operation between two bitsets
// using andNot()
bset1.andNot(bset2);
// printing the new bset1
System.out.println( "The resultant bset1 after andNot operation is : " + bset1);
}
} |
Output :
The elements of Bitset 1 are : {0, 1, 2, 3} The elements of Bitset 2 are : {0, 2, 4, 6} The resultant bset1 after andNot operation is : {1, 3}
3. or(Bitset set) : This method performs a logical OR of this target bit set with the argument bit set and returns the all values present in both bitset, doesn’t return duplicate elements.
Declaration : public void or(BitSet set) Parameters : set : a bit set Return Value : This method does not return a value.
// Java code to demonstrate the working // of or(Bitset set) in Bitset import java.util.*;
public class BitSetOr {
public static void main(String[] args)
{
// Declaring 2 bitsets
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
// adding the values to bset1
// using set()
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
// adding the values to bset2
// using set()
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
// printing the initial sets
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
// perform "or" operation between two bitsets
// using or()
bset1. or (bset2);
// printing the new bset1
System.out.println( "The resultant bset1 after or operation is : " + bset1);
}
} |
Output :
The elements of Bitset 1 are : {0, 1, 2, 3} The elements of Bitset 2 are : {0, 2, 4, 6} The resultant bset1 after or operation is : {0, 1, 2, 3, 4, 6}
4. xor(BitSet set) : This method performs a logical XOR and returns those elements that are present in one bitset but not in other.
Declaration : public void xor(BitSet set) Parameters : set a bit set. Return Value : This method does not return a value.
// Java code to demonstrate the working // of xor(Bitset set) in Bitset import java.util.*;
public class BitSetXor {
public static void main(String[] args)
{
// Declaring 2 bitsets
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
// adding the values to bset1
// using set()
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
// adding the values to bset2
// using set()
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
// printing the initial sets
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
// perform "xor" operation between two bitsets
// using xor()
bset1. xor (bset2);
// printing the new bset1
System.out.println( "The resultant bset1 after xor operation is : " + bset1);
}
} |
Output :
The elements of Bitset 1 are : {0, 1, 2, 3} The elements of Bitset 2 are : {0, 2, 4, 6} The resultant bset1 after xor operation is : {1, 3, 4, 6}