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
2
votes
1 answer

Testing ranges of bits in std::bitset

Is there an efficient way to test subranges in std::bitset? This seems to suggest not. I see std::bitset::any and std::bitset:all (for C++11), but they are for the whole set, not a subrange. What I was hoping for was an overloaded version of any…
Tim
  • 2,708
  • 1
  • 18
  • 32
2
votes
1 answer

The most efficient method to change the value of a range of bits in std::bitset

I need an efficient method with low complexity to change the values of a range of bits in the bitset : bitset<1000000> bs; and I need to set values from 100 to 500 to TRUE for example . what is the fastest method to accomplish this in the lowest…
Ebram Shehata
  • 446
  • 5
  • 20
2
votes
4 answers

create std::bitset or QBitArray from a std::string or QString containing hexadecimal numbers

Is there any way to construct a std::bitset from a hexadecimal std::string or QString and vise versa without performing binary shift operations? I know how to do it that way but I was wondering if it's possible to do this using C++ streams or…
max
  • 2,627
  • 1
  • 24
  • 44
1
vote
1 answer

binary logical operations with long bit sequences

C++ Problem: The minimum number of 1's should be found in the result of the XOR operation between a randomly generated N Bit sequence (N up to 50) std::bitset visit{0b01...110}; and a sequences of 1's, starting from std::bitset
Suslik
  • 929
  • 8
  • 28
1
vote
1 answer

How to perform bitwise operations on bitsets of different sizes?

Is there a way of performing bitwise operations on two std::bitsets of different sizes? Something similar to this: std::bitset<8> payload { 0b1010'0010 }; std::bitset<10> segment { 0b00'0000'0000 }; segment |= payload; For the above…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
1
vote
1 answer

Read bitset from file using istream_iterator

I'm trying to read a text file into a vector of std::bitset-objects. The file consists of 1's and spaces (for indicating 0 or false) and I'm trying to do this by overloading the input operator>> and using istream_iterators. I start by changing the…
user202542
  • 211
  • 1
  • 8
1
vote
1 answer

Is it necessary to check range in bit representation C++

Some data are stored in a 64 bit integer. As you can see, inside the getDrvAns function I check if the bit in the position drvIdx-1 is 0 or 1. One cannot be sure if the drvIdx will have a value in the right range (1-64). However, I noticed that if…
AngelosFr
  • 115
  • 1
  • 9
1
vote
1 answer

How to create bitset with size from input

I want to create a bitset with a size that I input, something like this: int a; int main() { cin >> a; const int l = a; cout << bitset(123); } But when I run this code I receive these errors: jdoodle.cpp:11:21: error: the value of…
David W
  • 158
  • 1
  • 2
  • 9
1
vote
0 answers

storing a std::bitset to disk takes up too much memory

I have difficulties finding out why my bitset takes up 29MB of memory when writing it to disk. I.e. the file that is written out is 29MB large. I write out the bitset in the following way: #include #include #include…
GoldenRetriever
  • 155
  • 3
  • 11
1
vote
1 answer

How to extract a subset of a bitset larger than unsigned long long without using to_string or extracting bits one-by-one?

I want to extract a subset from a given bitset, where: both G(given bitset size) and S(subset size) are known at compile-time G > sizeof(unsigned long long) * 8 S <= sizeof(int) * 8 The subset may start anywhere in the given bitset The resulting…
SacrificerXY
  • 324
  • 1
  • 9
1
vote
1 answer

Why are the bit patterns not matching with the use of std::bitset

While I was unit testing my class and its constructors I noticed something peculiar with my outputs. #include #include #include #include typedef std::uint8_t u8; typedef std::uint16_t u16; typedef…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
1
vote
0 answers

is there any data structure in python which is a substitute for bitset in c++?

I have written a code in c++ like this: struct ds{ bitset<3> counter; bitset<29> encoding; } vector lists; I want to have an array of ds,where each cell can store 3 bits counter and 29 bits encoding separately. I…
1
vote
2 answers

Why does MSVC not use __popcnt in its implementation for std::bitset::count?

I was curious to see whether or not MSVC used the compiler intrinsic __popcnt for bitset::count. Looking around, I found this to be the implementation for std::bitset::count for VS2017: size_t count() const _NOEXCEPT { // count number of…
Mike
  • 241
  • 1
  • 4
  • 11
1
vote
2 answers

Template deduction from std::bitset::operator[]

How does this code not compile? Why can bs[1] not be deduced to bool? Is there a generic way to solve this problem? #include #include #include using namespace std; template struct…
Finomnis
  • 18,094
  • 1
  • 20
  • 27
1
vote
2 answers

Using Bitset to display 16 bits into four groups of 4 bits in C++

I'm working on a programming assignment and I am using the bitset<> function in C++ to print put the binary representation of an integer by 16 bits. I am having a hard time trying to print the 16 bits into four groups of four bits with a space in…
Nicole
  • 37
  • 4