I have two strings as follows :
STRING1 : 011011110011000
STRING2 : 011001000001000
EXPECTED OUTPUT : 000010110010000
However, when i try to XOR them(bit-wise) using the following code, the output is blank. Code:
for(int i = 0; i<15; i++)
{
final_key[i] = STRING1[i] ^ STRING2[i];
cout<<" XOR = "<<final_key[i];
}
Any help would be appreciated.
You are trying to XOR 2
char
at a time. Try instead:Explanation
Refer to here for ASCII values.
The ASCII value for
'0'
is 48 and the ASCII value of'1'
is 49.48 ^ 49
is 1,48 ^ 48
and49 ^ 49
is 0. These would return a value of 0 or 1 to achar
, which would stand for either aEOF
char (if it is 0) or aSOH
char (if it is one), neither of which are output correctly.Thus you would want to convert each
char
into a bit (0 or 1) before conducting the XOR operation. Thus you can subtract'0'
from eachchar
to get the numrical value of the digit, conduct the XOR operation, then add back'0'
to get a proper outputc++ has
std::bitset<>
The ASCII values of the characters '0' and '1' are 48 and 49. To apply an XOR on two characters a,b ∈ { '0', '1' } you may use:
You are Xoring characters.That works but you are storing the result as it is without converting the result into character.
You could also check whether
s1[i]
is not equal tos2[i]
, then result is1