# What does the ^ = operator mean in C #?

12

I have a function in C #, where I decrypt a string and need to convert to a function in SQL Server for technicians to be able to work with the decrypted value.

There is a foreach , which I do not understand how it works:

``````var calculoChave = 0;
foreach (var c in chave)
{
calculoChave ^= c;
}
``````

What does this "^=" mean, and what does it do?

By debugging the code, I realized that when the key has a number, it adds up to 48. Equally, if the key is equal to 3, the value of the key calculation will be 51. If the key has more than 2 numbers, the calculation, and I did not find a logical sequence for it.

asked by anonymous 27.01.2015 / 13:32

9

This is XOR operator . In case it does an auto assignment.

``````calculoChave ^= c;
``````

is the same as

``````calculoChave = calculoChave ^ c;
``````

T-SQL has the same operator . As far as I know it does not have the shape contracted but now you know how to expand it.

27.01.2015 / 13:45
9

Only by completing @Maniero's response, since you are doing `foreach` on a `String` , that means you are traversing the character of `chave` and performing an XOR between the value of the `calculoChave` and the decimal value of the current character, just after the result is stored in the `calculoChave` variable itself, for example:

``````chave = "ab"
caractere "a"; decimal = 97; binário = 0110 0001
caractere "b"; decimal = 98; binário = 0110 0010
valor 0 (zero);              binário = 0000 0000
``````

It would look like this:

``````1ª iteração
0000 0000   (00)  0       calculoChave
0110 0001   (97) 'a'      caractere atual
---------   (XOR)
0110 0001   (97) 'a'      calculoChave = 97

2ª iteração
0110 0001   (97) 'a'      calculoChave
0110 0010   (98) 'b'      caractere atual
---------   (XOR)
0000 0011   (03)          calculoChave = 3
``````

You can check the ASCII table and look at the decimal and binary value of each character, note that there is a difference between the value 0 (zero) and the character "0" (zero).

27.01.2015 / 15:05