What is ^ ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is ^ ?

what does ^ sign means in C#. I have tried it with integer and the results were odd. Please help. 0 ^ 2 = 2 , 1 ^ 2 = 3 , 2 ^ 2 = 0, 3 ^ 2 = 1 ?????

21st Nov 2016, 6:32 PM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
4 Answers
+ 8
it is the XOR operator, it's a bitwise operator which works on the bit level of the variables XOR returns True if the bits on two corresponding indexes are not the same, and False otherwise: True XOR True = False False XOR False = False True XOR False = True False XOR True = True let's examine some of your examples: i will represent the variables with their bits equivalents using 4 bits and we need to find x each time: 0 ^ 2 = x 0 = 0000 2 = 0010 x = 0010 = 2 1 ^ 2 = x 1 = 0001 2 = 0010 x = 0011 = 3 2 ^ 2 = x 2 = 0010 2 = 0010 x = 0000 = 0 3 ^ 2 = x 3 = 0011 2 = 0010 x = 0001 = 1 hope i clarified it rather than making it worse :p
21st Nov 2016, 7:20 PM
Burey
Burey - avatar
+ 4
sure thing :D
22nd Nov 2016, 7:26 AM
Burey
Burey - avatar
+ 3
@Burey thanks for helping me in this issue. I was totally confused. You have cleared my concept. I will learn more about bitwise operators. Again thanks for the answer.
22nd Nov 2016, 7:24 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 1
well explained.
22nd Nov 2016, 7:12 AM
Martin K.
Martin K. - avatar