BitSet and(BitSet) in Java

and(BitSet): This method is available in java.util.BitSet class of Java.

Syntax:

void java.util.BitSet.and(BitSet set)

This method takes one argument of the type BitSet as its parameters. This method performs a logical AND of this target bit set with the argument bit set.

Parameters: One parameter is required for this method.

set: a bit set.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.BitSet;

public class BitSetand {
    public static void main(String[] args) {
        BitSet bitSet = new BitSet(4);

        bitSet.set(1);
        bitSet.set(5);
        bitSet.set(6);
        bitSet.set(10);

        BitSet bitSet2 = new BitSet(4);

        bitSet2.set(5);
        bitSet2.set(1);
        bitSet2.set(9);
        bitSet2.set(8);

        bitSet.and(bitSet2);

        System.out.println(bitSet);
    }
}

Output:

{1, 5}


No comments:

Post a Comment