4

I work with some data, it is represented as a sequence of bits,
number(378) of which isn't divisible by 8.

What are possible ways to work with such sequence?
I need to store it, process it, append one sequence to the other.

Options I considered:

  1. round up to the nearest number % 8 == 0, and use array of chars of that size.
  2. use bitfield, as far as I know memory alignment for bitfield is implementation defined.

Update:
Properties of bit sequences I am working with:
it is a codeWord that is produced from the given word using Reed-Solomon encoding algorithm.
Hence to work with bits as independent values is of no real use for me, my main concern is how to store codeWords and to append a new codeWord to the existing ones, as in order to reduce effect of burst errors I create interleaving matrix

spin_eight
  • 3,925
  • 10
  • 39
  • 61
  • Will appending be common? What sort of queries to you do on them in the processing? – harold May 18 '16 at 12:35
  • Option 1 sounds good. Are you worried about wasting memory? The wasted padding bits would only be about 1.5% of the space used. – samgak May 19 '16 at 03:45

2 Answers2

0

Another option is, to use std::vector. The std::vector of type bool is specialized on bool to only use ⌈v.size()/8⌉ bytes. But I am not saying you should use this. This specialization is the reason v.begin() is not just a raw pointer, but some type dependent iterator. In all other cases v.begin() of a vector is always a pointer to the element.

In case the length of the bitarray is very long, but you expect only very few bits to be set, you should also concider, to use std::set<int> instead, that stores the indices of the bits that are supposed to be true.

Arne
  • 7,921
  • 9
  • 48
  • 66
-1

There are several ways to represent a bit sequence in C++.

One, intended for sequences of a fixed size, is std::bitset:

// Create array
std::bitset<378> bitArray;
// Access a bit
bitArray[14] = 1;
// Check whether a bit is set
if (bitArray.test(14))

If you need dynamically resizable sequences, you should consider std::vector. If storage size is important and some access time overhead is not an issue, consider std::vector<bool> which is guaranteed by the standard to store each bool as a single bit in a tightly packed array.

If you don't care about packing tightly, you could also define your own Bit enumeration (or just use a char):

enum Bit : unsigned char
{
  Zero = 0, One = 1
}
std::vector<Bit> bitArray;
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Could you give a rough overview of how to append a bitset to another? – eerorika May 18 '16 at 10:29
  • @user2079303 `std::bitset` has a fixed size: it's an array of bits. You'd have to code such functionality yourself. – Angew is no longer proud of SO May 18 '16 at 10:30
  • 1
    @user2079303 ["Concatenate boost::dynamic_bitset or std::bitset"](http://stackoverflow.com/questions/3061721/concatenate-boostdynamic-bitset-or-stdbitset) – HostileFork says dont trust SE May 18 '16 at 10:36
  • @Angew, thanks for reply. For me std::bitset is one of many of possible ways to work with info where individual bit represents independent value. Here important how I get this sequence of bits: it is the codeWord that is produced from the given word using Reed-Solomon encoding algorithm. For me it is more valuable not to be able to work with individual bits, but to be able to form array of such sequences by appending every following to the result – spin_eight May 18 '16 at 10:55
  • @spin_eight Sorry, I was misled by the exact number given in your question and didn't at first notice the "appending" part. I've expanded the answer to include some dynamic versions. – Angew is no longer proud of SO May 18 '16 at 11:39