BitSet is a class defined in the java.util package. It creates an array of bits represented by 0s and 1s.
Syntax
public boolean intersects(BitSet set)
Parameter: The method accepts a mandatory parameter set, which is the BitSet to be checked for intersection.
Returns: The method returns a boolean indicating whether this BitSet intersects the specified BitSet
Explanation: The method returns true if the specified BitSet has any bits set to true that are also set to true in this BitSet, otherwise false is returned.
Examples:
Input :
set1 : {1, 2, 4}
set2 : {}
Output :
false
// Java program illustrating Bitset Class functions. import java.util.*; public class GFG { public static void main(String[] args) { // Constructors of BitSet class BitSet bs1 = new BitSet(); BitSet bs2 = new BitSet(); BitSet bs3 = new BitSet(); /* 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); System.out.println("bs3 : " + bs3); // Performing intersects on two bitset System.out.println("bs1 intersection bs3 = " + bs1.intersects(bs2)); System.out.println("bs3 intersection bs1 = " + bs3.intersects(bs1)); } } |
bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}
bs3 : {}
bs1 intersection bs3 = true
bs3 intersection bs1 = false
Related Articles :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced 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 BitSet | and
- Java BitSet | XOR
- Java BitSet | or()
- Java.util.BitSet.set() method in Java
- Java.util.BitSet.flip() in Java
- Java.util.BitSet.clear() in Java
- Java BitSet | cardinality()
- BitSet isEmpty() method in Java
- BitSet previousSetBit() method in Java
- BitSet nextClearBit() method in Java
- BitSet nextSetBit() method in Java
- BitSet previousClearBit() method in Java
- BitSet toString() Method in Java with Examples
- BitSet size() Method in Java with Examples
- BitSet clone() Method in Java with Examples
- BitSet equals() Method in Java with Examples
- BitSet length() Method in Java with Examples
- BitSet hashCode 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.

