std::bitset <1> a1;
std::bitset <1> a2;
a1 = std::bitset<1> (0);
a2 = std::bitset<1> (1);
std::bitset<1> b = (a1 ^= a2)
This results in
b = 1
which is fine but modifies also a1, which after the XOR operation becomes:
a1 = 1
Why is this happening? How can I avoid this without creating temp variables?
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
std::bitset <1> a1;
std::bitset <1> a2;
a1 = std::bitset<1>(0);
a2 = std::bitset<1>(1);
std::bitset<1> b = (a1 ^ a2);
std::cout << b << std::endl;
std::cout << a1 << std::endl;
std::cout << a2 << std::endl;
return 0;
}
Correct output: 1 0 1
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
std::bitset <1> a1;
std::bitset <1> a2;
a1 = std::bitset<1>(0);
a2 = std::bitset<1>(1);
std::bitset<1> b = (a1 ^= a2);
std::cout << b << std::endl;
std::cout << a1 << std::endl;
std::cout << a2 << std::endl;
return 0;
}
Correct output:
1
1
1
because ^=
, so you change a1.
XOR is operator ^
Operator ^=
is XOR with assignment