Scala BitSet &() method with example

Scala Bitsets are sets of non-negative integers which are represented as variable-size arrays of bits packed into 64-bit words. The &() method is utilised compute the intersection between this bitset and another bitset by performing a bitwise “and”.

Method Definition: def &()

Return Type: It returns a new bitset consisting of all elements that are both in this bitset.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Bitset &
// method 
import scala.collection.immutable.BitSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
  
        val b1: BitSet = BitSet(41, 12, 23, 43, 1, 72
        val b2: BitSet = BitSet(1, 100, 5, 12, 23
  
        // Applying BitSet &() function 
        val bs1: BitSet = b1 & (b2)
          
        // Displays output 
        println(bs1
      
    

chevron_right


Output:

BitSet(1, 12, 23)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Bitset &
// method 
import scala.collection.immutable.BitSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
  
        val b1: BitSet = BitSet(41, 12, 23, 43, 1, 72
  
        // Applying BitSet &() function 
        val bs1: BitSet = b1 & BitSet(1, 100, 5, 12, 23
          
        // Displays output 
        println(bs1
      
    

chevron_right


Output:

BitSet(1, 12, 23)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.