The and()
function of the BitSet
class in Java is used to perform the logical AND operation.
We can invoke this method by using the dot (.
) operator on the invoking object, which will then perform the AND
operation with the BitSet
object provided as an argument.
The result of the AND
operation will be visible through the invoking object.
public void and(BitSet bitset)
To use this method, you must import the java.util.BitSet
class in your program, as shown below.
import java.util.BitSet
bitset
: An instance or object of the BitSet
class.
This method does not return any value.
The code below shows how the and()
function works in Java.
Tip: Execute this code multiple times for different results.
import java.util.BitSet;import java.util.Random;class EdPresso {public static void main( String args[] ) {// object for Random ClassRandom rand = new Random();// objects of BitSet ClassBitSet bitset1 = new BitSet(15);BitSet bitset2 = new BitSet(15);// set some bitsfor(int i = 0; i < 10; i++) {bitset1.set(rand.nextInt(30));bitset2.set(rand.nextInt(30));}System.out.println("bitset1: ");System.out.println(bitset1);System.out.println("\nbitset2: ");System.out.println(bitset2);// AND bitsbitset1.and(bitset2);System.out.println("\nbitset1 AND bitset2: ");System.out.println(bitset1);}}
We have two BitSet
objects in the code above: bitset1
and bitset2
.
In line , we use an object of the Random class to generate random numbers for each BitSet
object.
The and()
method in line , bitset1.and(bitset2)
, returns the logical AND
between bitset1
and bitset2
. The result can be viewed by printing bitset1
, as it is altered to store the result of the AND
operation.