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 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 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 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 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 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 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.
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}
Similar Reads
Java Logical Operators with Examples
Logical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consider
8 min read
Java.util.BitSet.clear() in Java
There are three variants of clear() method: clear() : The clear() method sets all of the bits in this BitSet to false. public void clear() Return Value This method does not return a value. // Java code to demonstrate the working // of clear() in BitSet import java.util.*; public class BitClr1 { publ
3 min read
Java.util.BitSet.set() method in Java
There are four variants of set() method.This article depicts about all of them, as follows: 1. set(int Index) : This method sets the bit at the specified index to true i.e adds a value. Declaration : public void set(int bitIndex) Parameters : Index : a bit index. Result : This method does not return
3 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.
6 min read
Java.util.BitSet.flip() in Java
There are two variants of flip() method. This article depicts about all of them as follows: 1. flip(int value) : This method removes the value specified in the argument. public void flip(int value) Parameters : value : the value to flip. Return ValueThis method does not return a value. // Java code
2 min read
Java.util.BitSet class in Java with Examples | Set 1
BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Constructors: BitSet class Constructors / \ BitSet() BitSet(int no_Of_Bits)BitSet() : A no-argument constructor to create an empty BitSet object. BitSet(int no_Of_Bits): A one-constructor w
2 min read
Java.util.BitSet class methods in Java with Examples | Set 2
Methods discussed in this post: BitSet class methods. / / | | \ \ set() xor() clone() clear() length() cardinality() We strongly recommend to refer below set 1 as a prerequisite of this. BitSet class in Java | Set 1 set() : java.util.BitSet.set() method is a sets the bit at the specified index to th
4 min read
Bitwise Right Shift Operators in Java
In C/C++ there is only one right shift operator '>>' which should be used only for positive integers or unsigned integers. Use of the right shift operator for negative numbers is not recommended in C/C++, and when used for negative numbers, the output is compiler dependent. Unlike C++, Java su
2 min read
Java.util.BitSet.get() in Java
There are two variants of get() in Bitset, both are discussed in this article. 1. boolean get( int value ) : Returns true if the value is present in Bitset, else returns false. Declaration : public boolean get(int value) Parameters : value : The value to check. Return Value : Returns boolean true, i
2 min read
BitSet previousSetBit() method in Java
BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values. Prerequisite : Java BitSet | Set 1 Bitset.previousSetBit() This method is used to find whether there are any true bits that occur on or before the specified starting index. This function re
3 min read