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
6
votes
2 answers

How to pass bitsets with different sizes to a function?

I want to pass bitsets to a function. What size should I assign to the bitset parameter bits in the function prototype if the bitsets have different sizes? For example: bitset<3> a; bitset<4> b; void ABC(bitset bits){ …
pcoder
  • 401
  • 1
  • 7
  • 18
5
votes
1 answer

How can I use a bitset on the heap in C++?

If i use a bitset on the stack i can do the following: std::bitset<8> bset_s; bset_s.flip(1); std::cout << "Bitset on stack: " << bset_s << std::endl; std::cout << "Element 1: " << bset_s[1] << std::endl; Output: Bitset on stack: 00000010 Element…
nubilot
  • 83
  • 3
5
votes
2 answers

Difference of behavior between libstdc++ and libc++: operator>> on bitset

Consider the following code: #include #include #include int main(int argc, char* argv[]) { std::stringstream stream; std::bitset<1> bitset(1); std::cout<<"before = "<
Vincent
  • 57,703
  • 61
  • 205
  • 388
5
votes
2 answers

split std::bitset in two halves?

I am implementing DES algorithm and I need to split std::bitset<56> permutationKey in two halves. std::bitset<56> permutationKey(0x133457799BBCDF); std::bitset<28> leftKey; std::bitset<28> rightKey; std::bitset<56>…
Abdul Rehman
  • 1,687
  • 18
  • 31
5
votes
1 answer

Why does the C++ standard specifies a destructor for std::bitset::reference?

I am wondering why std::bitset::reference and std::vector::reference specifies an explicit destructor (not compiler generated one). Because, for example, boost::dynamic_bitset::reference does not seem to specify such a destructor.
Vincent
  • 57,703
  • 61
  • 205
  • 388
4
votes
0 answers

How can I deduce function template arguments of type std::bitset::reference?

I am trying to create a function overload for std::bitset::reference. I know this usually isn't useful since implicit conversions to/from bool already exist, but I believe it is necessary here (although I am open to other suggestions). As an…
4
votes
3 answers

Using scanf/printf to input into/output from a bitset

I'm somewhat new to C++, and I was wondering how to scanf into or printf out of a bitset, i.e., what is the appropriate type specifier for I/O to a bitset index? An example of what I would want to do is: #include #include using…
skang
  • 41
  • 3
4
votes
2 answers

constexpr for values passed in by reference

I had this piece of code which compiles #include struct A{ std::bitset<50> b; }; void test(A a){ static_assert(sizeof(int)*8 < a.b.size(), "can't accomodate int in bitset"); int x = 5; a.b = x; } int main(){ A a; …
Zoso
  • 3,273
  • 1
  • 16
  • 27
4
votes
2 answers

C++ How can I assign an input value to a std::bitset argument?

I want to make a simple program that will take number of bits from the input and as an output show binary numbers, written on given bits (example: I type 3: it shows 000, 001, 010, 011, 100, 101, 110, 111). The only problem I get is in the second…
januszysko
  • 41
  • 1
  • 3
4
votes
2 answers

Best c++ way to choose randomly position of set bit in bitset

I have std::bitset<32> word and I want to choose randomly and index (0-31) of some bit which is 1. How can I do that without loops and counters. Is there any std::algorithm suitable for that? If it's easier I can convert the bitset to string or int…
Hanna Khalil
  • 975
  • 1
  • 10
  • 28
4
votes
4 answers

Change integer in bitset

How does one change the integer being used by bitset? Suppose I used bitset to declare a variable mybitset which stores the bits of a number, say 32. After doing some operations, I want mybitset to store the bits of some other number, say 63. How do…
therainmaker
  • 4,253
  • 1
  • 22
  • 41
4
votes
3 answers

Convert bitset to signed int, with a<32>

I was reading the question convert bitset to int in c++ and thought, hey, that doesn't work, I've already tried it. But then I started trying and quickly i discovered that: #include #include int main() { std::bitset<31>…
Schaki
  • 1,697
  • 1
  • 12
  • 14
3
votes
4 answers

What is the fastest way to get the lowest set bit of a C++ std::bitset?

I noticed that std::bitset does not have a function for returning or popping the lowest set bit of a bitset (nor the highest set bit for that matter). What is the fastest way to accomplish this task, specifically for std::bitset objects not…
qq4
  • 282
  • 4
  • 14
3
votes
3 answers

Initializing very large C++ std::bitset at compile time

I want to store a static constant bitset of 216 bits, with a specific sequence of 1s and 0s that never changes. I thought of using an initializer string as proposed by this post : std::bitset<1<<16> myBitset("101100101000110 ... "); // the ellipsis…
3
votes
1 answer

What's the best data structure to use for a sieve (i.e. list of numbers where some get crossed out)?

I am implementing the Sieve of Eratosthenes. I am stuck on the first step: deciding which data structure to use. Put simply, the Sieve of Eratosthenes starts with a list of consecutive numbers (e.g. 2,3,4,5,...99, 100). Then, certain numbers are…