Java BitSet and() method

The and() method of Java BitSet class is used to perform a logical AND operation of this target bit set with the specified set argument. The value of bit set is true if and only if the bit set of both initially and the corresponding bit argument has the true value.

Syntax:

Parameter:

DataTypeParameterDescription
BitSetsetIt is a bit set.

Returns:

NA

Exception:

NullPointerException - If null parameter will pass in the method.

Compatibility Version:

Java 1.0 and above

Example 1

Output:

bitset1: {0, 1, 2, 3, 4}
bitset2: {2, 4, 6, 8, 10}
result bitset: {2, 4}

Example 2

The BitSet class converts the character into a byte value.

Output:

bitset1: {4, 5, 97, 98, 99}
bitset2: {3, 4, 6, 97, 98}
result bitset: {4, 97, 98}