Questions tagged [std-bitset]

A C++ container class similar to an array but containing only bits, included in the library.

A C++ container class similar to an array but containing only bits, included in the <bitset> library. It has a fixed length specified at compile time via a template parameter.

It allows for random access and can be manipulated with standard logic operators.

A reference is available.

109 questions
3
votes
1 answer

How can I cast an std::bitset to an std::bitset?

If I have an std::bitset<16>, how can I convert it into an std::bitset<32> with the upper bits filled with 0? std::bitset<16> a = 0xFF00; std::bitset<32> b = a; // error
Eric
  • 95,302
  • 53
  • 242
  • 374
3
votes
3 answers

Why does std::bitset only support integral data types? Why is float not supported?

On trying to generate the bit pattern of a float as follows: std::cout << std::bitset<32>(32.5) << std::endl; the compiler generates this warning: warning: implicit conversion from 'double' to 'unsigned long long' changes value from 32.5 to 32…
cedoc
  • 33
  • 6
3
votes
1 answer

Compiler returning an error when passing a const variable: template argument is not a constant expression

So I wrote this code -> #include #include int main(){ int num, temp, digits = 0; std::cin >> num; temp = num; while(temp){ temp /= 10; ++digits; } const int size = digits; …
Shubham Anand
  • 128
  • 10
3
votes
1 answer

Can I use std::bitset's functions with OpenACC?

Is it possible to use bitset's functions in OpenACC region? An example code: #include #include #pragma acc routine seq int mystrcmp (const char *, const char *); int main(int argc, char** argv) { long sum = 3, i; …
fokhagyma
  • 73
  • 1
  • 7
3
votes
1 answer

Bitset as the return value of a function

I'd like to have an interface whose function returns a bitset: class IMyInterface { public: virtual std::bitset<100> GetBits() = 0; }; The problem is that I don't want to force the size of the bitset. So I think I have to use…
B Faley
  • 17,120
  • 43
  • 133
  • 223
2
votes
1 answer

Using enum class with std::bitset and combining bitsets

I am trying to make write a code for i2c communication with a device (ADS1115). The communication uses different arrays of bits for commands and sending data back, in which most bits or group of bits have different meanings. So, I did the natural…
2
votes
0 answers

What is wrong with my equivalent code from vector to bitset

I was solving this problem on HackerRanked and found the following solution: #include #include using namespace std; int main() { constexpr size_t BIG = 1u<<31; vector arr(BIG); size_t N,S,P,Q; cin >> N >> S…
2
votes
1 answer

std:bitset c++ external initialization

I want to wrap or cast a std:bitset over a given constant data arrary or to formulate it differently, initialize a bitset with foreign data. The user knows the index of the bit which he can check then via bitset.test(i). Data is big, so it must be…
2
votes
1 answer

Any hope to convert a std::bitset<128> to a single 128-bit integer in the near future?

I am using std::bitset to represent short DNA sequences (haplotypes). For my purposes, I need to be able to convert each such sequence into a single integer. At the moment, it seems like this requirement bounds my sequences to be of length <=64…
gvbarroso
  • 21
  • 4
2
votes
1 answer

Struggling to create a Boost-Bimap containing std::bitset

I have a number of strings and their bitset equivalents. I need to be able to look up equivalents in both directions, i.e. "str to bitset" and "bitset to str". I believe boost-bimap would be the right container for this job. I managed to get this to…
W.R.
  • 23
  • 3
2
votes
1 answer

I'm using std::bitset and trying to create two arrays std::bitset with size 100,000,000,000

I am using std::bitset and I try to create two arrays std::bitset with size 100,000,000,000. As a result, program fills only 298 MB of my RAM but must to fill ~24 GB. I have 32 GB RAM and now 26 GB are free. When I build my code for x86, it…
kileatb
  • 35
  • 6
2
votes
1 answer

Overload the shift operators of std::bitset

I'd like to use the shift operators as doing a bit rotation instead of their actual bit shift. This is my expected behavior: std::bitset<8> b8("1010"); // b8 = 00001010 b8 <<= 5; // b8 = 01000001 So I try and overloaded the <<=…
홍한석
  • 439
  • 7
  • 21
2
votes
2 answers

Bitset, vector of bool or vector of ints for simple big integer

I have an algorithm where I currently use two unsigned integers as bitmaps to store information about the input; this limits the maximum input size to 64, so I'd like to create a version where the integers are replaced by a bitset or simple big…
2
votes
1 answer

c++ bitset logical operations in O(log n)?

According to this post Performance of doing bitwise operations on bitsets performance is O(n) how do I make it O(log n). Thanks.
moghya
  • 854
  • 9
  • 18
2
votes
1 answer

Converting a bitset to signed

I have a std::bitset<32> and I want to isolate the right 16 bits and output those bits as if they were a signed number. I also am going to want to output the entire 32 bit thing as a signed number down the road. However, Bitset does not support a…
ViscousRandom
  • 179
  • 1
  • 9