Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the complexity of C++ bitset constructor that converts from long?

My guess is O(n) where n is the no. of bits. Or is it constant w.r.t. n? I mean it shouldn’t it just be able to copy the bits from memory?

like image 376
Foon Avatar asked Aug 27 '18 13:08

Foon


People also ask

What does Bitset return in C++?

The bitset::any() is an inbuilt function in C++ STL which returns True if at least one bit is set in a number. It returns False if all the bits are not set or if the number is zero. Parameter: The function does not accepts any parameter.

How much memory does Bitset use?

As expected, the BitSet with the same number of bits consumes around 1 KB, which is far less than the boolean[]. The above code will compute the object size for both types of bit-vectors with different lengths.

Is Bitset faster than an array of bools?

As bitset stores the same information in compressed manner the operation on bitset are faster than that of array and vector.

How do I convert Bitset to all bits to 1?

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.


1 Answers

Mathematically speaking, long has a fixed length, therefore copying it's contents is constant-time operation. On the other hand, you need to zero-out the rest of the bits in the bitset and that you are not able to do in less-than-linear time with respect to the length of the bit_set. So, In theory you cannot do better than O(n), where n is the length of the bitset.

I guess that from the asymptotical complexity point of view you can safely assume that the complexity of the constructor is the same as zeroing-out the allocated memory.

This analysis however has some value only for huge values of n and it does not make much sense to me to use a long constructor to initialize a bitset of million bits. So, if the size of the bitset is on the same scale as the size of long, it is practically constant-time.

like image 160
jlanik Avatar answered Sep 30 '22 09:09

jlanik