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
305
votes
13 answers

How to print (using cout) a number in binary form?

I'm following a college course about operating systems and we're learning how to convert from binary to hexadecimal, decimal to hexadecimal, etc. and today we just learned how signed/unsigned numbers are stored in memory using the two's complement…
Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
92
votes
1 answer

Why is libc++'s vector::const_reference not bool?

Section 23.3.7 Class vector [vector.bool], paragraph 1 states: template class vector { public: // types: typedef bool const_reference; ... However this program fails to compile when…
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
39
votes
2 answers

Why doesn't std::bitset come with iterators?

It appears that std::bitset does not come with STL iterators. Therefore, I cannot do the following: std::bitset<8> bs; for (auto it: bs) { std::cout << "this can not be done out of the box\n"; } Instead I must: std::bitset<8> bs; for…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
15
votes
2 answers

Is there a more elegant way to convert a two-state string into a bitset than just by a 'for' loop?

I wrote this code snippet to convert a two-state string ("+++--+-" or "yynnny") into a std::bitset: #include #include #include #include std::bitset<70> convertTwoStateString(std::string twoState) { …
Suslik
  • 929
  • 8
  • 28
15
votes
2 answers

in bitset, can i use "to_ulong" for a specific range of bits?

I'm working on something that requires me to get access to specific bits and ranges of bits. I decided to use bitset because it is easy to get access to specific bits; how can I extract a range (subset) of bits?
jimmy
  • 151
  • 1
  • 3
14
votes
3 answers

Are enums the canonical way to implement bit flags?

Currently I'm using enums to represent a state in a little game experiment. I declare them like so: namespace State { enum Value { MoveUp = 1 << 0, // 00001 == 1 MoveDown = 1 << 1, // 00010 == 2 MoveLeft = 1 << 2, // 00100 == 4 …
Yohaï-Eliel Berreby
  • 1,165
  • 3
  • 14
  • 25
14
votes
2 answers

How to initialize a bitset type all bits to 1

I know set() function for a already constructed bitset object, but I need a constructed bitset which all bits are 1. The situation is a default function parameter. for example: void bar(std::bitset<100> flags = X) { } what X should be, -1 may works…
user2729541
  • 153
  • 1
  • 1
  • 4
13
votes
2 answers

boost::dynamic_bitset slower than std::bitset unless std::bitset is reset

I recently came across the bitset templates and would really like to use them in my current project. Reading on, I see that the std::bitset template must have a size determined at compile time. Many suggest using the boost::dynamic_bitset to…
nick
  • 319
  • 1
  • 4
  • 18
9
votes
2 answers

How to convert a range subset of bits in a C++ bitset to a number?

I have an std::bitset and the bitset type also provides a to_ulong method to translate the bitset into a number, my problem is about translating the bitset into a number while just considering a range in that bitset, I need to implement my own…
user2485710
  • 9,451
  • 13
  • 58
  • 102
8
votes
2 answers

How to access range of bits in a bitset?

I have a bitset which is very large, say, 10 billion bits. What I'd like to do is write this to a file. However using .to_string() actually freezes my computer. What I'd like to do is iterate over the bits and take 64 bits at a time, turn it into…
Terence Chow
  • 10,755
  • 24
  • 78
  • 141
8
votes
1 answer

What is the complexity of C++ bitset constructor that converts from long?

My guess is O(n) where n is the no. of bits. Or is it constant w.r.t. n? I mean it shouldn’t it just be able to copy the bits from memory?
Foon
  • 221
  • 1
  • 7
8
votes
1 answer

Using enum class with std::bitset

First of all I want a normal enumeration instead of a bit-based enumeration, because the amount of different enums will be beyond any integral type. I also want to take advantage of the type safety of C++11 enum class. To do so, the natural choice…
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
6
votes
3 answers

Convert a 74-bit integer to base 31

To generate a UFI number, I use a bitset of size 74. To perform step 2 of UFI generation, I need to convert this number: 9 444 732 987 799 592 368…
thibsc
  • 3,747
  • 2
  • 18
  • 38
6
votes
4 answers

How to safely offset bits without undefined behaviour?

I'm writting a function that will convert a bitset to a int/uint value considering that the bitset could have fewer bits than target type. Here is the function I wrote: template static T convertBitSetToNumber( const…
jpo38
  • 20,821
  • 10
  • 70
  • 151
6
votes
5 answers

std::bitset::all substitute for prior C++11 compilers

I would like to use the std::bitset::all but unfortunately my compiler is pre-dated C++11. I know that I could mimicate the functionality by checking in a loop whether all bits of my std::bitset are set. e.g., template
101010
  • 41,839
  • 11
  • 94
  • 168
1
2 3 4 5 6 7 8