Is there a way of performing bitwise operations on two std::bitset
s of different sizes?
Something similar to this:
std::bitset<8> payload { 0b1010'0010 };
std::bitset<10> segment { 0b00'0000'0000 };
segment |= payload;
For the above example, I basically want to transfer all the 8 bits of payload
to the lower bits of segment
. What other options do I have if this cannot be done? Should I write a for loop that uses the subscript operator[]
to transfer the bits one by one?
CodePudding user response:
Convert the narrower to the wider one then you can use |=
:
#include <iostream>
#include <bitset>
int main() {
std::bitset<8> payload { 0b1010'0010 };
std::bitset<10> segment { 0b00'0000'0000 };
std::bitset<10> x(payload.to_ullong());
segment |= x;
std::cout << segment;
}
For wider ones you can use to_string
and the constructor creating the bitset from a string.