How to perform logical andNot operation on BitSet in Java ?
Declaration :
1 |
public void andNot(BitSet set) |
Explanation :
Purpose | The andNot(BitSet set) method clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet. |
Parameters | set ===> the BitSet with which to mask this BitSet. |
Return Value | This method does not return a value. |
Exception | NA |
Java Program : Example
Below example will explain java.util.BitSet.andNot() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.c4learn; import java.util.*; public class BitSetExample { public static void main(String[] args) { // creating two bitsets BitSet bitsetObj1 = new BitSet(8); BitSet bitsetObj2 = new BitSet(8); // Assign values to bitsetObj1 bitsetObj1.set(10); bitsetObj1.set(11); bitsetObj1.set(12); bitsetObj1.set(13); bitsetObj1.set(14); bitsetObj1.set(15); // Assign values to bitsetObj2 bitsetObj2.set(12); bitsetObj2.set(14); bitsetObj2.set(16); bitsetObj2.set(18); bitsetObj2.set(10); // Print all Bitsets System.out.println("Value of Bitset Obj 1 : " + bitsetObj1); System.out.println("Value of Bitset Obj 2 : " + bitsetObj2); // andNot operation bitsetObj1.andNot(bitsetObj2); // print the new bitsetObj1 System.out.println("andNot Result : " + bitsetObj1); } } |
Output of Program :
1 2 3 |
Value of Bitset Obj 1 : {10, 11, 12, 13, 14, 15} Value of Bitset Obj 2 : {10, 12, 14, 16, 18} andNot Result : {11, 13, 15} |