C # xor functionality

I found this code to change a string using an operator or,

public static string ReverseXor(string s)
{

      if (s == null) return null; 
      char[] charArray = s.ToCharArray(); 
      int len = s.Length - 1;

      for (int i = 0; i < len; i++, len--)
      { 
            charArray[i] ^= charArray[len]; 
            charArray[len] ^= charArray[i]; 
            charArray[i] ^= charArray[len]; }

       //some more code
}

The problem is that I don’t understand what is going on inside the for loop, can someone explain this to me?

Thank.

+5
source share
5 answers

Here you can replace the two values ​​A, B without a temporary intermediate variable:

A = A Xor B
B = A Xor B
A = A Xor B

Link: XOR sharing algorithm

Here is an example of 8 bits:

A = 10010010
B = 01111001

A = A Xor B = 11101011
B = A Xor B = 10010010
A = A Xor B = 01111001
+5
source

The method uses the "old trick" to replace variables that are all, it is equal to:

char temp = charArray[i];
charArray[i] = charArray[len];
charArray[len] = temp;

It is used to easily create the creation of a new variable " temp" for exchange.

+3
source

, , . ? ?

, .

xor. . , , .

+1

XOR.

X Y:

X := X XOR Y
Y := X XOR Y
X := X XOR Y

. .

0

. XOR . XOR-ing X Y Y. .

XOR .

0

All Articles