Java Utility Library

Java BitSet - or() Method



The java.util.BitSet.or() method is used to perform a logical OR operation on the given BitSet with the argument BitSet. The given BitSet is modified so that each bit in it has the value true if either BitSet initially had the value true for the corresponding bit.

Syntax

public void or(BitSet set)

Parameters

set Specify the BitSet.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.BitSet.or() method is used to perform a logical OR operation on the given BitSet called BSet1 with the argument BitSet BSet2.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating BitSets
    BitSet BSet1 = new BitSet();
    BitSet BSet2 = new BitSet();

    //populating BSet1
    BSet1.set(10);
    BSet1.set(20);
    BSet1.set(30);
    BSet1.set(40);
    BSet1.set(50);

    //populating BSet2
    BSet2.set(20);
    BSet2.set(40);
    BSet2.set(60);
    BSet2.set(80);
    BSet2.set(100);    

    //printing BitSet
    System.out.println("Before OR operation.");
    System.out.println("BSet1 contains: " + BSet1);

    //Performing OR operation on BSet1 using BSet2
    BSet1.or(BSet2);

    //printing BitSet
    System.out.println("\nAfter OR operation."); 
    System.out.println("BSet1 contains: " + BSet1);   
  }
}

The output of the above code will be:

Before OR operation.
BSet1 contains: {10, 20, 30, 40, 50}

After OR operation.
BSet1 contains: {10, 20, 30, 40, 50, 60, 80, 100}

❮ Java.util - BitSet