Search code examples
c++bitset

How to reassign a bitset?


#include <iostream>
#include <bitset>

using namespace std;

int main()
{
    // a = 5(0000000101)
    unsigned char a = 5;
 
   bitset<10> y(a);
   cout<<y<<endl;
   
   //Using left shift operator a = 10(0000001010)
   a = a<<1;
   
   bitset<10> z(a);
   cout<<z<<endl;

}

I want to set the bitset y as the new refreshed bitset since I applied the left shift to a and not have to create a new bitset using the constructor every time. (In this case I made a new bitset z.)

Is there a way to reassign the value a to the bitset?


Solution

  • Bitsets are CopyAssignable. Simple assignment works:

    y = a<<1;