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
1
vote
1 answer

initialize vector of pair of (bitset,int)

when initializing with vector > > arr; arr.push_back(make_pair(x,y)); but I am gettig error: ‘pair’ cannot appear in a constant-expression vector > > arr; how to make a vector of pair of bitset,int?
1
vote
1 answer

g++ out of memory allocating for std::bitset

Here I'm allocating 10^9 bits: #include #include const int N = 1000000000; std::bitset b; int main() { std::cout << sizeof(b) << std::endl; } I get cc1plus.exe: out of memory allocating 268439551 bytes. But when I do…
qwr
  • 9,525
  • 5
  • 58
  • 102
1
vote
1 answer

XOR bitset when 2D bitset is stored as 1D

To answer How to store binary data when you only care about speed?, I am trying to write some to do comparisons, so I want to use std::bitset. However, for fair comparison, I would like a 1D std::bitset to emulate a 2D. So instead of…
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1
vote
2 answers

Variable bit length in std::bitset

I'm not sure how to properly explain this, but I'm looking for a way to automatically set the size or number of the bitset automatically Example cout << bitset<8>(7) << endl; outputs with a fixed number of bits 0000 0111 I want to…
1
vote
2 answers

write std::bitset to binary file and load the file to std:bitset

I am working with a project which will use bitset. As the text file provided is very large(>800M), to load it directly to std::bitset will cost more then 25 seconds. So I want to preprocess the text file to a memory dumped binary file. Because a…
1
vote
1 answer

is bitset data stored in reverse order?

I was trying out std::bitset and after getting wrong results for a while I noticed that the results were in reverse order. Tried searching on cppreference page but couldn't find any source on this and hence need an confirmation. This should be…
Abhinav Gauniyal
  • 7,034
  • 7
  • 50
  • 93
1
vote
1 answer

convert uint8_t array to bitset in C++

Is there a quick way to convert an array of uint8_t to a biteset. uint8_t test[16]; // Call a function which populates test[16] with 128 bits function_call(& test); for(int i=0; i<16; i++) cout<
CPP_NEW
  • 197
  • 2
  • 9
1
vote
2 answers

Bit reversal for N bit word using c++ constexpr

I am working on a bit reversal algorithm for an fft implementation, my implementation so far is //assume the proper includes template unsigned long&& bitreverse(unsigned long value){ std::bitset input(value); …
Alex Zywicki
  • 2,263
  • 1
  • 19
  • 34
1
vote
1 answer

Use a std::bitset or a fundamental type of the same size?

I'm creating a Chess Solver and have decided to use bitboards. Conveniently there are 64 squares on a standard chess board. This is nice since the prevalence of 64-bit operating systems a single bitboard can fit into a single register. That said,…
Casey
  • 10,297
  • 11
  • 59
  • 88
1
vote
3 answers

Printing an int in binary with fixed length

I'm overloading << to print custom objects (in this case, instances of a custom class Vertex). As part of this, I want to print a given integer in binary. I'd prefer for many reasons to case with std::bitset rather than run a for loop, but the…
PengOne
  • 48,188
  • 17
  • 130
  • 149
1
vote
1 answer

C++ std::map std::bitset segfault

I have this code: static void XMLCALL hackHandler(void *data, const XML_Char *name, const XML_Char **attr) { SetPointers* sets = static_cast(data); if (strcmp(name, "instruction") == 0 || strcmp(name, "load") == 0 || strcmp(name,…
adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
0
votes
0 answers

Why C++ iterator into vector of bitset gets heap-use-after-free from AddressSanitizer

I'm having an issue with using iterators into vector>. The following minimal program gets a heap-use-after-free warning from the AddressSanitizer. #include #include using namespace std; int main(int argc, char const…
R-penguins
  • 36
  • 4
0
votes
0 answers

"error: expected identifier before numeric constant" when trying to define a std::bitset inside a class

I'm trying to create a variable of type std::bitset<16> in my class: #include class foo{ public: std::bitset<16> bar (0xfa2); }; But the program fails to compile with the following error: $ g++ src/test.h src/test.h:5:26: error:…
Jan
  • 25
  • 6
0
votes
0 answers

How do I initialize a bitset in array order?

A std::bitset isn't an integer, it's an array of bits. But when I initialize it with the bits in the correct order (as I would an array), it seems that all of the bits get reversed like an Endian swap. The pattern I want is: [1] 0 1 0 1 1 0 1 …
Nick
  • 10,904
  • 10
  • 49
  • 78
0
votes
2 answers

2 dimensional bitset and square bracket operator

For a personal project I need to use a 2D bitset. Conducted a lot of googling and found a really great post with very useful code, https://forums.codeguru.com/showthread.php?522711-Need-2-dimensional-Bitarray. Take a look at the answer by…
flashburn
  • 4,180
  • 7
  • 54
  • 109