STL bitset

Started by
4 comments, last by Deyja 18 years, 7 months ago
hi I am working on my bitpacking implementation and want to use STL bitset to store the bits I have examined the code of the bitset the data is stored in an array of unsigned long s I have 2 questions 1. Is it guaranteed that there won t be any additional variables in other STL implementations ? So i could simple do a reinterpret_cast on the pointer to send the bitpack over the network. 2. I miss the possibility to set n bits of a int at position idx in the bitset e.g.: for(i=0;i<n;i++) m.set(idx+i,value&(1<<i); is there a similar feature? what i could think of is a second bitset secondset = std::bitset<nbits>(value); firstset |= (secondset << idx); but I would rather prefer the first implementation since my bitsets will be around 150-300 bytes alrge == 2400 bits
http://www.8ung.at/basiror/theironcross.html
Advertisement
It would need to store atleast the size and the actual data. Sending it over the network like that is a really bad idea; the bit-data is almost certainly allocated dynamically. All you'll get on the other side is a pointer to who-knows-where.
Quote:Original post by Deyja
It would need to store atleast the size and the actual data. Sending it over the network like that is a really bad idea; the bit-data is almost certainly allocated dynamically. All you'll get on the other side is a pointer to who-knows-where.

I doubt it, at least VC6's implementation doesn't. Remember that bitsets use a fixed size controlled by a template argument.

Even so treating a bitset this way may not be such a great idea, even if they happen to work in most cases. For one it won't be portable to other platforms (you may encounter both bit and byte-order issues) and it's not very efficient when working with multiple bits either (such as the operation you describe).
Consider writing your own alternative bitset instead. It really isn't that hard..
I prefer boost::dynamic_bitset if you need bitsets of varying sizes. It has functions allowing you to retrieve blocks of bits out of it as ulong or something. You could pack these into a buffer and send them. If your number of bits is static and you need to send it around like that I might prefer just using a struct with a bitfield inside.
i already thought about the bitfield but unfortunatly the data isn t static
i have aready written a little bitset maybe i just refine it a bit

thx
http://www.8ung.at/basiror/theironcross.html
Ah, boost's dynamic bitset was what I was thinking of. I've never actually used the standard one. I didn't relieze it was static (Though... well. Duh. ...)

This topic is closed to new replies.

Advertisement