Search code examples
c++binarybitbitset

How to get bitset OR with different bitset sizes


When I use,

  std::bitset<5> op1 (std::string("01001"));
  std::bitset<5> op2 (std::string("10011"));

  std::cout << (op1|=op2) << std::endl;  

everything is fine obviously.

But my question is, how can I use OR operation using two different sized 'std::bitset's? Such as,

  std::bitset<11> op1 (std::string("101110011"));
  std::bitset<5> op2 (std::string("01001"));

  std::cout << (op1|=op2) << std::endl;

I can't compile this code in VS 2012. What have I missed here? Can't I use different sized bitsets for OR operations (and XOR as well)? Is this a platform specific problem?


Solution

  • The interface doesn't support this directly. You could construct a temporary bitset instead:

    // assuming op1 is larger
    op1 |= std::bitset<op1.size()>(op2.to_ullong())
    

    If the larger bitset's value wouldn't fit in a unsigned long long, youl could call to_string instead.