×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet &~() Method in Scala

By IncludeHelp Last updated : November 09, 2024

BitSet in Scala is a special collection of positive integers. Scala programming language has a huge library containing a lot of utility functions to help working with data structure easy.

BitSet &~() method

BitSet &~() method is used to find the difference between two bitsets i.e. find the elements present in BitSet1 but not in BitSet2.

Syntax

BitSet1.&~(BitSet2)

Parameters

It accepts a single parameter which is the BitSet with which the difference is calculated.

Return Types

The method returns a BitSet which contains elements that are present in BitSet1 but not in BitSet2.

Example 1

Illustrate the working &~() method

// Program to illustrate the working of 
// BitSet &~() method in scala...

import scala.collection.immutable.BitSet

object myObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(4, 1, 6, 2, 9)
        val BitSet2 = BitSet(1, 3, 7, 8, 6)
    
        println("The elemets of BitSet1 : " + BitSet1)
        println("The elemets of BitSet2 : " + BitSet2)
    
        val resBitSet = BitSet1 &~(BitSet2)
    
        println("The resultant BitSet of  'BitSet1 &~(BitSet2)' is " + resBitSet)
    }
}

Output

The elemets of BitSet1 : BitSet(1, 2, 4, 6, 9)
The elemets of BitSet2 : BitSet(1, 3, 6, 7, 8)
The resultant BitSet of  'BitSet1 &~(BitSet2)' is BitSet(2, 4, 9)

Explanation

In the above code, we have created two BitSets BitSet1 and BitSet2, and print its value. Then we have used the &~() method and returned the difference of both.

Example 2

Illustrate the working of &~() method when BitSet1 is a subset BitSet2

// Program to illustrate the working of 
// BitSet &~() method in scala

import scala.collection.immutable.BitSet

object myObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(4, 1, 6, 2, 9)
        val BitSet2 = BitSet(1, 3, 8, 6, 2, 9, 4)
    
        println("The elemets of BitSet1 : " + BitSet1)
        println("The elemets of BitSet2 : " + BitSet2)
    
        val resBitSet = BitSet1 &~(BitSet2)
        println("The resultant BitSet of  'BitSet1 &~(BitSet2)' is " + resBitSet)
    }
}

Output

The elemets of BitSet1 : BitSet(1, 2, 4, 6, 9)
The elemets of BitSet2 : BitSet(1, 2, 3, 4, 6, 8, 9)
The resultant BitSet of  'BitSet1 &~(BitSet2)' is BitSet()

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.