Java BitSet xor() method

The xor(BitSet set) method of Java BitSet class is used to perform a logical XOR operation on the set bits of this set with specified set argument. The value of bit set is modified so that each bit in it has true if and only if any one of the following statements satisfies:

  • If the initial bit value holds true and the corresponding bit in the argument holds false value.
  • If the initial bit value holds false and the corresponding bit in the argument holds 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, 4, 6, 7}
bitset2: {2, 4, 6, 8, 10}
xor operation between bitsets: {0, 1, 2, 7, 8, 10}

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.xor(Unknown Source)
	at BitSetXorExample2.main(BitSetXorExample2.java:15)
bitset1: {0, 1, 4, 6, 7}