How to perform logical AND operation on BitSet in Java ?
Declaration :
1 |
public void and(BitSet set) |
Explanation :
Purpose | The and(BitSet set) method performs a logical AND of this target bit set with the argument bit set. This bit set is modified so that each bit in it has the value true if and only if it both initially had the value true and the corresponding bit in the bit set argument also had the value true. |
Parameters | set ===> a bit set |
Return Value | This method does not return a value. |
Exception | NA |
Java Program : Example
Below example will explain java.util.BitSet.and() 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 the bitset System.out.println("Value of Bitset Obj 1 : " + bitsetObj1); System.out.println("Value of Bitset Obj 2 : " + bitsetObj2); // and operation bitsetObj1.and(bitsetObj2); // Print the bitsetObj1 System.out.println("and operation 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} and operation result : {10, 12, 14} |