I am using a bitset that is created in the following way
std::bitset<4> bitset;
I wanted to know how I can assign a value to a bitset if I have ints with the
values A=0,B=1,C=1,D=0
?
I have read that I could do this
bitset.set(0, false);
bitset.set(1, true);
bitset.set(2, true);
bitset.set(3, false);
I wanted to know if there was a faster way for this ? Preferably a single statement ?
By example
std::bitset<4> bitset(6UL);
I wanted to know how I can assign a value to a bitset if I have ints with the values A=0,B=1,C=1,D=0 ?
If you have multiple variable (one variable for every bit) I suppose the best you can do is assign every single bit, how do you know
bitset.set(0, (A != 0));
bitset.set(1, (B != 0));
bitset.set(2, (C != 0));
bitset.set(3, (D != 0));
or, simpler,
bitset.set(0, A);
bitset.set(1, B);
bitset.set(2, C);
bitset.set(3, D);
If you really want initialize with a single statement (and if the variable have only 0
and 1
values), I suppose you can use the bitshift
std::bitset<4> bitset((A << 3)|(B << 2)|(C << 1)|D);