How do I initialize a bitset in array order?

494 Views Asked by At

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  0  1  0  1  // [1] should be array Element[0].

I initialize with this:

std::bitset<12> bMyBitset("101011010101");

But when I use a function that prints out the array elements, I get this:

// Backwards:
[0] = 1, [1] = 0, [2] = 1, [3] = 0, [4] = 1, [5] = 0, [6] = 1, [7] = 1, [8] = 0, [9] = 1, [10] = 0, [11] = 1

What I want is this:

// Forward:
[0] = 1, [1] = 0, [2] = 1, [3] = 0, [4] = 1, [5] = 1, [6] = 0, [7] = 1, [8] = 0, [9] = 1, [10] = 0, [11] = 1

I know I can manually put all the bits backwards in the initializer string, but that is extremely tedious and error prone. I'm looking for a solution where I can put them in the same order that I would when initializing an array, with Element[0] first.

I want to do this:

std::bitset<12> bMyBitset {1,0,1,0,1,1,0,1,0,1,0,1};
// no matching function for call to 'std::bitset<12>::bitset(<brace-enclosed initializer list>)'

How can I initialize a bitset with elements in array order?

0

There are 0 best solutions below