Prerequisite : Java BitSet | Set 1
BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.
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.
Syntax
public void and(BitSet set);
Examples:
Input :
set1 : {1, 2, 4}
set2 : {2, 3, 4}
Output : After performing st1.and(set2)
set2 : {2, 4}
Program:
// Java program illustrating Bitset Class constructors. import java.util.*; public class GFG { public static void main(String[] args) { // Constructors of BitSet class BitSet bs1 = new BitSet(); BitSet bs2 = new BitSet(6); /* set is BitSet class method expalined in next articles */ bs1.set(0); bs1.set(1); bs1.set(2); bs1.set(4); // assign values to bs2 bs2.set(4); bs2.set(6); bs2.set(5); bs2.set(1); bs2.set(2); bs2.set(3); // Printing the 2 Bitsets System.out.println("bs1 : " + bs1); System.out.println("bs2 : " + bs2); // Performing logical AND // on bs2 set with bs1 bs2.and(bs1); // bs2 set after Performing AND System.out.println("\nAfter AND operation : "); System.out.println(bs2); } } |
bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}
After AND operation :
{1, 2, 4}
Related Articles :
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.BitSet.clear() in Java
- Java.util.BitSet.set() method in Java
- Java.util.BitSet.flip() in Java
- Java.util.BitSet.get() in Java
- BitSet class methods in Java with Examples | Set 3
- Java.util.Bitset class | Logical operations
- BitSet isEmpty() method in Java
- BitSet previousSetBit() method in Java
- Java BitSet | or()
- Java BitSet | XOR
- Java BitSet | cardinality()
- Java BitSet | intersects()
- BitSet nextClearBit() method in Java
- BitSet nextSetBit() method in Java
- BitSet previousClearBit() method in Java
- BitSet clone() Method in Java with Examples
- BitSet equals() Method in Java with Examples
- BitSet length() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

