- and / notand : java.util.BitSet.and() and java.util.BitSet.notand() method is a java.util.Bitset class method.
.and() method performs logical AND operation of bit set(target) with the bit set passed as an argument. This will return a bit (set) if and only if both the bits that are being operated are true.
.notand() method – All those bits having it’s corresponding bit – Set are cleared using this method
Syntax:
public void and(BitSet bitset)
or
public void andNot(BitSet bitste)
Parameters:
bitset - set to perform the operation
- equal : java.util.BitSet.equal() method plays its role in comparing two bitset. It does so by comparing an object with other.
Syntax:
public boolean equals(Object o)
Parameters:
o - the object to compare with
Overrides: equals in class Object
Return: if object are same then true; otherwise false
- get() : java.util.BitSet.get() method creates a new BitSet with elements from the given Bitset having positions from from_Index (inclusive) to_Index(exclusive).
Syntax:
public BitSet get(int from_Index, int to_Index)
Parameters:
from_Index - index of the first bit of given BitSet to include
to_Index - index after the last bit of the BitSet to include
Throws:
IndexOutOfBoundsException - if from_Index is negative, or to_Index is negative,
or from_Index is larger than to_Index
Java code explaining the use of and(), notand(), equal(), get() methods.
import java.util.*;
public class GFG
{
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
bs1.set( 7 );
bs1.set( 1 );
bs1.set( 2 );
bs1.set( 4 );
bs1.set( 3 );
bs1.set( 6 );
bs2.set( 4 );
bs2.set( 6 );
bs2.set( 3 );
bs2.set( 9 );
bs2.set( 2 );
System.out.println( "bs1 : " + bs1);
System.out.println( "bs2 : " + bs2);
System.out.println( "\nUse of get() on bs1 : "
+ bs1.get( 1 , 4 ));
System.out.println( "Use of get() on bs2 : "
+ bs2.get( 1 , 4 ));
bs1.andNot(bs2);
System.out.println( "\nNot b/w bs1 and bs2 : " + bs1);
bs1.and(bs2);
System.out.println( "And b/w bs1 and bs2 : " + bs1);
if (bs1.equals(bs2))
System.out.println( "\nUsing equal method : Equal" );
else
System.out.println( "\nUsing equal method : Not Equal" );
}
}
|
Output:
bs1 : {1, 2, 3, 4, 6, 7}
bs2 : {2, 3, 4, 6, 9}
Use of get() on bs1 : {0, 1, 2}
Use of get() on bs2 : {1, 2}
Not b/w bs1 and bs2 : {1, 7}
And b/w bs1 and bs2 : {}
Using equal method : Not Equal
- flip() : java.util.BitSet.flip(from_Index, to_Index) method sets each bit from the given from_Index (inclusive) to the specified to_Index (exclusive) to current value’s compliment.
Syntax:
public void flip(int from_Index, int to_Index)
Parameters:
from_Index - index of the first bit of the given BitSet to flip
to_Index - index after the last bit of the given BitSet to flip
Throws:
IndexOutOfBoundsException - if from_Index is negative, or to_Index is negative,
or from_Index is larger than to_Index
- intersect() : java.util.BitSet.intersect() method returns true if both the bits in target BitSet and the given BitSet are set.
Syntax:
public boolean intersects(BitSet set)
Parameters:
set - BitSet to intersect with
Returns: true if the given BitSet intersects the target BitSet
- isEmpty() : java.util.BitSet.isEmpty() method whether there is any bit, that is set to true or not in the given Bitset.
Syntax:
public boolean isEmpty()
Return: boolean indicating whether the given BitSet is empty
Java code explaining the use of intersect(), isEmpty(), flip() methods.
import java.util.*;
public class NewClass
{
public static void main(String[] args)
{
BitSet bs1 = new BitSet();
BitSet bs2 = new BitSet();
bs1.set( 7 );
bs1.set( 1 );
bs1.set( 2 );
bs1.set( 4 );
bs1.set( 3 );
bs1.set( 6 );
bs2.set( 4 );
bs2.set( 6 );
bs2.set( 3 );
bs2.set( 9 );
bs2.set( 2 );
System.out.println( "bs1 : " + bs1);
System.out.println( "bs2 : " + bs2);
System.out.println( "" );
System.out.println( "Using intersect() : "
+ bs2.intersects(bs1));
bs1.flip( 1 , 5 );
System.out.println( "\nbs1 using flip : " + bs1);
bs2.flip( 2 , 4 );
System.out.println( "bs2 using flip : " + bs2);
System.out.println( "" );
System.out.println( "Use of isEmpty() : "
+ bs1.isEmpty());
System.out.println( "Use of isEmpty() : "
+ bs2.isEmpty());
}
}
|
Output:
bs1 : {1, 2, 3, 4, 6, 7}
bs2 : {2, 3, 4, 6, 9}
Using intersect() : true
bs1 using flip : {6, 7}
bs2 using flip : {4, 6, 9}
Use of isEmpty() : false
Use of isEmpty() : false
References :
https://docs.oracle.com/javase/7/docs/api/java/util/BitSet.html
Last Updated :
16 Aug, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...