Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is bitset guaranteed contiguity?

Swift and easy question: is std::bitset guaranteed to be contiguous in memory?

I know it abides by CopyConstructible and CopyAssignable concepts, but is it also a ContiguousContainer (or something like that) like std::vector?

Apart from padding, I'd like to make bitwise operations on structures like this:

struct tmp
{
    std::bitset<32> b;
    unsigned int    c;
};

So the contiguity of b is quite important. Of course, this leads to knowing if std::bitset is a standard layout class, so that every bitwise operation works.

like image 281
senseiwa Avatar asked Apr 08 '16 13:04

senseiwa


People also ask

What does the BitSet function do?

Introduction. 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.

Is BitSet faster than vector bool?

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

How much memory does BitSet use?

Memory Footprint As shown above, this boolean[] consumes around 10 KB of memory. As expected, the BitSet with the same number of bits consumes around 1 KB, which is far less than the boolean[].

How large can a BitSet be?

The maximum element in the set is the size - 1st element. The default size of the bit set is 64-bit space. If the bit is set at index larger than the current BitSet size, it increases its bit space in the multiplication of 64*n, where n starts from 1, 2, 3, so on.


2 Answers

There is no requirement in the standard for std::bitset<> to have any particular layout or bit order.

The fact that the number of bits required is specified as the template argument does imply that memory is allocated as one contiguous block (an array member). Because allocating more than one block would be more complex, less efficient and serve no useful purpose.

Nevertheless, I do not suggest or endorse accessing the internal representation of std::bitset<>.

like image 97
Maxim Egorushkin Avatar answered Oct 26 '22 23:10

Maxim Egorushkin


There is no such guarantee. std::bitset has, in my experience, an awkward interface, so I don't see much of the point of using it in undefined ways either.

Just write your own bitset like class with the layout and storage guarantees you need.

Personally, I'd enjoy it. And I don't find the bitset interface (especially construction/serialization/deserialization) particularly good, so I wouldn't even feel guilty. (I guess writing an efficient count, any, all and none is a bit of work, but everything else is trivial. And if you are writing assembly instructions/using SSE intrinstics, you might be able to match/exceed your compiler's implementation anyhow)

like image 35
Yakk - Adam Nevraumont Avatar answered Oct 26 '22 23:10

Yakk - Adam Nevraumont