What is BitSet or(BitSet set) in Java?

The void method or() is used to perform the logical OR operation. We can invoke this method with the dot (.) operator on invoking objects and performing the OR operation with the argument BitSet bitset object. The result of an OR operation will be returned through invoking an object, i.e., bitset1.or(bitset2).

This built-in BitSet class implements a vector of bits, available at java.util.*.

Syntax


public void or(BitSet set)

Parameters

set: The method accepts objects of BitSet type.

Return value

The method does not return any object.

Example

In the code snippet below, we use the java.util.Random and java.util.BitSet classes to generate random numbers and bitset objects, respectively. Therefore, we have two objects (bitset1, bitset2) of the BitSet class. In line 20, we apply an OR logical operation and store the resultant value in invoking object bitset1.

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(20);
BitSet bitset2 = new BitSet(20);
// set some bits
for(int i = 0; i < 10; i++) {
bitset1.set(rand.nextInt(30));
bitset2.set(rand.nextInt(30));
}
System.out.println("bitset1: ");
System.out.println(bitset1);
System.out.println("\nbitset2: ");
System.out.println(bitset2);
// OR bits
bitset1.or(bitset2);
System.out.println("\nbitset1 OR bitset2: ");
System.out.println(bitset1);
}
}

Free Resources