Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java programming
communitycreator

What is BitSet xor(BitSet set) in Java?

Fouzia Bashir

The xor() method in the BitSet class is used to perform XOR with this bit set (invoking) and the bit set argument. The result of logical XORExclusive OR will be assigned to the this.bitset bit set.

XOR is a two digital input circuit that results in 1 when both input values differ.

Syntax

This is used to perform logical XOR between two BitSet objects:


void xor(BitSet set)

Parameters

set: The argument value of BitSet type.

Return value

This method has no return type. It is defined as void in the built-in BitSet class.

Example

As highlighted below, we have two BitSet objects, bitset1 and bitset2, with at least 8 random values, each using the java.util.Random class. As a result, we are doing logical XOR between bitset1 and bitset2 in line 21.

import java.util.BitSet;
// Random Class for random number generator
import java.util.Random;
class EdPresso {
    public static void main( String args[] ) {
      // object for Random Class
      Random rand = new Random();
      // objects of BitSet Class
      BitSet bitset1 = new BitSet(10);
      BitSet bitset2 = new BitSet(10);
      // set some bits
      for(int i = 0; i < 8; i++) {
         bitset1.set(rand.nextInt(20));
         bitset2.set(rand.nextInt(20));
      }
      System.out.println("bitset1: ");
      System.out.println(bitset1);
      System.out.println("\nbitset2: ");
      System.out.println(bitset2);
      // XOR operation on bits
      bitset1.xor(bitset2);
      System.out.println("\nbitset1 OR bitset2: ");
      System.out.println(bitset1);
    }
}

RELATED TAGS

java programming
communitycreator
RELATED COURSES

View all Courses

Keep Exploring