In the scaladoc, BitSet
is defined as extending Set[Int]
. So I thought using a BitSet
as in instance of Set[Int]
would work, but I get a type mismatch:
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
scala> import collection.BitSet
import collection.BitSet
scala> val b: Set[Int] = BitSet()
<console>:8: error: type mismatch;
found : scala.collection.BitSet
required: Set[Int]
val b: Set[Int] = BitSet()
^
However a cast works:
scala> val b: Set[Int] = BitSet().asInstanceOf[Set[Int]]
b: Set[Int] = BitSet()
So why do I need to explicitely cast a BitSet
to a Set[Int]
while Set[Int]
is a super-type of Set[Int]
?
bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1.
Bitset is a container in C++ Standard Template Library for dealing with data at the bit level. 1. A bitset stores bits (elements with only two possible values: 0 or 1). We can however get the part of a string by providing positions to bitset constructor (Positions are with respect to string position from left to right)
Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.
This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Bitset – 1”. 1. What is bitset in C++? Explanation: Bitset is a collection of bool variables with each element consuming only one bit.
It turns out that your Set
is actually scala.collection.immutable.Set
. So you can
val b0: Set[Int] = collection.immutable.BitSet()
val b1: collection.Set[Int] = collection.BitSet()
val b2: collection.immutable.Set[Int] = collection.immutable.BitSet()
val b3: collection.mutable.Set[Int] = collection.mutable.BitSet()
val b4: collection.Set[Int] = collection.immutable.BitSet()
val b5: collection.Set[Int] = collection.mutable.BitSet()
but not any of
val x1: collection.immutable.Set[Int] = collection.BitSet()
val x2: collection.immutable.Set[Int] = collection.mutable.BitSet()
val x3: collection.mutable.Set[Int] = collection.BitSet()
val x4: collection.mutable.Set[Int] = collection.immutable.BitSet()
and it turns out that the default import for Set
gives you x2
. Import collection.immutable.BitSet
, or import collection.Set
(to cover collection.immutable.Set
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With