user1717140
user1717140

Reputation: 353

boost dynamic_bitset copy bits from uint16

I need to create 24 bit set. First (0) bit have to be set by bool. And other (1 - 23) I need to copy first bits from uint32 value

Is it possible to do it with dynamic_bitset ?

My code I tried but wrong:

typedef boost::dynamic_bitset<unsigned char> DataType;
DataType bs(24, intValue);
bs.set(0, booleanValue);

Upvotes: 2

Views: 442

Answers (2)

sehe
sehe

Reputation: 392833

Just left-shift:

    DataType bs(24, intValue);
    bs <<= 1;
    bs.set(0, boolValue);

Live On Coliru

#include <boost/dynamic_bitset.hpp>
#include <iostream>
typedef boost::dynamic_bitset<unsigned char> DataType;

int main() {
    using namespace std; // for readability on SO
    cout << hex << showbase;

    uint32_t intValue = 0x666;
    cout << "input: " << intValue;

    DataType bs(24, intValue);

    cout << "\n#1: " << bs << " " << bs.to_ulong();

    bs <<= 1;
    cout << "\n#2: " << bs << " " << bs.to_ulong();

    bs.set(0, true);

    cout << "\n#3: " << bs << " " << bs.to_ulong();
}

Prints:

input: 0x666
#1: 000000000000011001100110 0x666
#2: 000000000000110011001100 0xccc
#3: 000000000000110011001101 0xccd

Upvotes: 1

user1717140
user1717140

Reputation: 353

So, I managed to do it in this way, without boost bitset:

uint32_t buffer(0xAAAAAAAA);
buffer = buffer << 1;
buffer |= true << 0;

unsigned char newRecord[3];

newRecord[0] = buffer;
newRecord[1] = buffer << 8;
newRecord[2] = buffer << 16;

Upvotes: 0

Related Questions