Java BitSet andNot() method

The andNot() method of Java BitSet class is used to clear the entire bit in this BitSet whose corresponding bit is set in the specified BitSet.

Syntax:

Parameter:

DataTypeParameterDescription
BitSetsetIt is a bit set.

Returns:

NA

Exception:

NullPointerException - If null parameter will pass in the method.

Compatibility Version:

Java 1.2 and above

Example 1

Output:

bitset1: {0, 1, 2, 3, 4}
bitset2: {1, 3, 5, 6, 7}
result bitset after andNot: {0, 2, 4}

Example 2

This method throws a NullPointerException if we pass a null parameter.

Output:

Exception in thread "main" java.lang.NullPointerException
	at java.util.BitSet.andNot(Unknown Source)
	at BitSetAndNotExample2.main(BitSetAndNotExample2.java:16)
bitset1: {0, 1, 2, 3, 4}