I recently started playing around with C#, and I'm trying to understand why the following code doesn't compile. On the line with the error comment, I get:
Cannot implicitly convert type 'int' to 'char'. An explicit conversion exits (are you missing a cast?)
I'm trying to do a simple XOR operation with two strings.
public string calcXor (string a, string b)
{
char[] charAArray = a.ToCharArray();
char[] charBArray = b.ToCharArray();
char[] result = new char[6];
int len = 0;
// Set length to be the length of the shorter string
if (a.Length > b.Length)
len = b.Length - 1;
else
len = a.Length - 1;
for (int i = 0; i < len; i++) {
result[i] = charAArray[i] ^ charBArray[i]; // Error here
}
return new string (result);
}
result[i] =(char)((short) charAArray[i] ^ (short)charBArray[i]);bitwise operatordoes NOTtake 1 and 0. That would be aboolean operator, which only has two values. Abitwise operatoracts on ALL the bits of two integers, resulting in an integer. As seen in the accepted answer.