1
votes

I have a bitset<64> (16bit serial and 48bit timestamp), which I want to write to quint8 *data .

When the bitset<64>looks like:

1111001010100101...

I want data to look like:

data[0] = 11110010
data[1] = 10100101
...

Would be thankful if somebody could help me to implement that.

1
Finally, a std::bitset reference could be helpful. There are ways to get a 64-bit integer out of your bitset, which can then be used to copy the bits to your array of bytes. - Some programmer dude

1 Answers

1
votes

If I understood you correctly then this accomplishes what you ask:

#include <iostream>
#include <bitset>

int main()
{
    std::bitset<64> bits64{0b0111101111011001110011010101110001111011110110011100110101011100};

    /*
    01111011
    11011001
    11001101
    01011100
    01111011
    11011001
    11001101
    01011100
    */

    for (auto i = 7; i >= 0 ; i--)
        std::cout << std::bitset<8>{bits64.to_ullong() >> (i * 8)} << std::endl;

    return 0;
}

Output:

01111011
11011001
11001101
01011100
01111011
11011001
11001101
01011100

You can change the for loop statement to make it emit in whatever ordering of bytes you choose.

The key thing is employing the shift operator (<< or >>) on the integral value.