#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> input = {2,3,4,5,4,3,7,2};
int XOR = 0;
for (auto& i : input)
{
XOR = XOR ^ i;
}
return XOR;
}
This returns 2, when it should return 5. Can anyone please explain why?
Add some debug lines, and you will see what is happening inside. With the help of
std::formatyou can even see the binary representation of eachXORandidirectly in each iteration. https://gcc.godbolt.org/z/Te1786j5jPrints:
Now you see, it shouldn't be
5(i.e101), rather2(i.e.010).