0

I need to be able to store an array of binary numbers in c++ which will be passed through different methods and eventually outputted to file and terminal,

What are the key differences between vectors and bit sets and which would be easiest and/or more efficient to use?

(I do not know how many bits I need to store)

S.Mitchell
  • 91
  • 1
  • 2
  • 10
  • 3
    You may overthink your problem ([XY](http://xyproblem.info/)?). Hint: All numbers inside the computer are binary ;) – Ivan Aksamentov - Drop Nov 22 '16 at 15:44
  • 1
    bitsets are of a compile-time determined size, anyway. – jaggedSpire Nov 22 '16 at 15:45
  • You also mention an `array` - do you have a fixed size collection of numbers? What do you want to do with them? – doctorlove Nov 22 '16 at 15:45
  • 1
    All depends on what you want to do with those binary numbers. Can you give us an idea on what you're trying to achieve? – ShadowMitia Nov 22 '16 at 15:50
  • Are you planning on storing bits (or bools) in say a vector? If so, take a look at this: https://isocpp.org/blog/2012/11/on-vectorbool. Otherwise just store the numbers, and count the bits in each or whatever it is you are needing to do. – doctorlove Nov 22 '16 at 15:51

5 Answers5

2

std::bitset size should be known at compile time, so your choice is obvious - use std::vector<bool>. Since it's implemented not the same as std::vector<char> (since a single element takes a bit, not a full char), it should be a good solution in terms of memory use.

sandyre
  • 175
  • 2
  • 9
  • 2
    Take note of the specialization that makes this efficient, and read the docs. It does break some other assumptions about how a vector should behave. – Kenny Ostrom Nov 22 '16 at 15:57
1

It all depends on what you want to do binary.

You could also use boost.dynamic_bitset which is like std::bitset but not with fixed bits. The main drawback is dependency on boost if you don't already use it.

You could also store your input in std::vector<char> and use a bitset per char to convert binary notation.

As others already told: std::bitset uses a fixed number of bits.

std::vector<bool> is not always advised because it has its quirks, as it is not a real container (gotw).

0xCursor
  • 2,242
  • 4
  • 15
  • 33
stefaanv
  • 14,072
  • 2
  • 31
  • 53
0

If you don't know how many bits you need to store at compilation time, you cannot use bitset, as it's size is fixed. Because of this, you should you vector<bool>, as it can be resized dynamically. If you want an array of bits saved like this, you can then use vector< vector<bool> >.

Honza Dejdar
  • 947
  • 7
  • 19
0

I think that in your case you should use std::vector with the value type of std::bitset. Using such an approach you can consider your "binary numbers" either like strings or like objects of some integer type and at the same time it is easy to do binary operations like setting or resetting bits.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If you don't have any specific upper bound for the size of the numbers you need to store, then you need to store your data in two different dimensions:

  • The first dimension will be the number, whose size varies.
  • The second dimension will be your array of numbers.

For the latter, using std:vector is fine if you require your values to be contiguous in memory. For the numbers themselves you don't need any data structure at all: just allocate memory using new and an unsigned primitive type such as unsigned char, uint8_t or any other if you have alignment constraints.

If, on the other hand, you know that your numbers won't be larger than let's say 64 bits, then use a data type that you know will hold this amount of data such as uint64_t.

PS: remember that what you store are numbers. The computer will store them in binary whether you use them as such or with any other representation.

Jorge Bellon
  • 2,901
  • 15
  • 25