Java Utility Library

Java BitSet - andNot() Method



The java.util.BitSet.andNot() method is used to clear all of the bits in this BitSet whose corresponding bit is set in the argument BitSet.

Syntax

public void andNot(BitSet set)

Parameters

set Specify the BitSet with which to mask this BitSet.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.BitSet.andNot() method is used to clear all of the bits in the given BitSet called BSet1 whose corresponding bit is set in the argument BitSet called 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 andNot operation.");
    System.out.println("BSet1 contains: " + BSet1);

    //Performing AND operation on BSet1 using BSet2
    BSet1.andNot(BSet2);

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

The output of the above code will be:

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

After andNot operation.
BSet1 contains: {10, 30, 50}

❮ Java.util - BitSet