Java.util.Bitset class | Logical operations
Last Updated :
16 Aug, 2018
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.
import java.util.*;
public class BitSetAnd {
public static void main(String[] args)
{
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
bset1.and(bset2);
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.
import java.util.*;
public class BitSetNotAnd {
public static void main(String[] args)
{
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
bset1.andNot(bset2);
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.
import java.util.*;
public class BitSetOr {
public static void main(String[] args)
{
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
bset1. or (bset2);
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.
import java.util.*;
public class BitSetXor {
public static void main(String[] args)
{
BitSet bset1 = new BitSet( 5 );
BitSet bset2 = new BitSet( 5 );
bset1.set( 0 );
bset1.set( 1 );
bset1.set( 2 );
bset1.set( 3 );
bset2.set( 2 );
bset2.set( 4 );
bset2.set( 6 );
bset2.set( 0 );
System.out.println( "The elements of Bitset 1 are : " + bset1);
System.out.println( "The elements of Bitset 2 are : " + bset2);
bset1. xor (bset2);
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}
Share your thoughts in the comments
Please Login to comment...