bitsets

When to use STL bitsets instead of separate variables?

In what situation would it be more appropriate for me to use a bitset (STL container) to manage a set of flags rather than having them declared as a number of separate (bool) variables? Will I get a significant performance gain if I used a bitset for 50 flags rather than using 50 separate bool variables? ...

How best to implement BCD as an exercise?

I'm a beginner (self-learning) programmer learning C++, and recently I decided to implement a binary-coded decimal (BCD) class as an exercise, and so I could handle very large numbers on Project Euler. I'd like to do it as basically as possible, starting properly from scratch. I started off using an array of ints, where every digit of t...

Boolean[] vs. BitSet: Which is more efficient?

What is more efficient in terms of memory and CPU usage — an array of booleans or a BitSet? Specific BitSet methods are not used, only get/set/clear (==, =, Arrays.fill respectively for an array). ...

How to write a std::bitset template that works on 32 and 64-bit

Consider the following code template<unsigned int N> void foo(std::bitset<N> bs) { /* whatever */ } int main() { bitset<8> bar; foo(bar); return 0; } g++ complains about this on 64 bit because the <8> gets interpreted as an unsigned long int, which doesn't exactly match the template. If I change the template to say unsign...

Setting boost dynamic_bitset from a string

Dynamic bitset I have a use case where i need to populate boost::dynamic_bitset<unsigned char> , from a std::string buffer. Can you suggest as to how to go about this. So I need to come up with a function void populateBitSet (std::string &buffer, boost::dynamic_bitset<unsigned char> & bitMap) { //populate bitMap ...

dynamic_bit set print?

std::string charBuff = "11010"; dbitset = boost::dynamic_bitset<unsigned char> (charBuff); for (boost::dynamic_bitset<>::size_type i = 0; i < dbitset.size(); ++i) { std::cout << dbitset[i]; } It prints from the LSB to MSB. Output: 01011. What should I do to so that bitset is printed correctly. I can reverse the character buffer ...

How to subtract two bitsets in c++

Hi, i have 2 bitsets each one storing 100 bits. I'm trying to simply subtract with '-', but I always get a compilation error at this point. How do you subtract 2 bitsets in c++? Thanks in advance ...

Easy way to convert a string of 0's and 1's into a character? Plain C

I'm doing a steganography project where I read in bytes from a ppm file and add the least significant bit to an array. So once 8 bytes are read in, I would have 8 bits in my array, which should equal some character in a hidden message. Is there an easy way to convert an array of 0's and 1's into an ascii value? For example, the array: ch...

How do I change the value of a dynamic_bitset?

I am using C++ boost's dynamic_bitset. I have already allocated a variable and I just want to change its value - to construct it anew from an 'unsigned long' like from the constructor, but I don't want to allocate the memory again or to create a temporary variable. What can I do? ...

BitSet memory usage in Scala

I would like to know what is the memory usage of BitSet in Scala.For example, if I do: var bitArray:BitSet=new BitSet(10) bitArray.add(0) bitArray.add(2) bitArray.add(4) bitArray.add(6) bitArray.add(8) How does that compare with and array containing the even numbers 0,2,4,6,8? What about writing a number in binary: var...