Q:

bitxor on c++ strings

I'm trying to port the matlab-function bitxor to c++ to achieve a bitwise XOR-operation on std::strings. 
Now I'm not sure whether this actually works in general? If I take the strings and perform XOR on the individual characters, I observe the following:

c=xor(a, b); d=xor(a, c) works fine, i.e. d equals b.
"3" is bitwise 00110011 whereas int a=3 is bitwise 00000011. Thus, "3" xor "2" returns a character that can't be displayed but which equals 1.

Does anyone know whether - and if yes how - it is possible to perform this bitwise XOR on strings? It is used for network coding stuff.

A:

If you want to xor each character in the string, just iterate through the string and create new one:
std::string bitxor(std::string x, std::string y)
{
    std::stringstream ss;

    // works properly only if they have same length!
   for(int i = 0; i < x.length(); i++)
   {

       ss <<  (x.at(i) ^ y.at(i));
   }

    return ss.str();
}

int main()
{
    std::string a = "123";
    std::string b = "324";
    std::string c = bitxor(a, b);
    std::string d = bitxor(c, b);

    std::cout << a << "==" << d << std::endl; // Prints 123 == 123

    return 0;
}

