5

What is the operator below ^? When to use it?

My programing language is C#.

Gaby
  • 2,923
  • 4
  • 38
  • 52
  • 3
    Google, or MSDN in this case, is your friend: http://msdn.microsoft.com/en-us/library/zkacc7k1%28VS.71%29.aspx. To answer your 2nd question, you use the operator when you need it, just like e.g. the `+` operator. – stakx - no longer contributing Jul 22 '10 at 14:26
  • Google is not an answer, MSDN, however, is. http://meta.stackexchange.com/questions/8724/how-to-deal-with-google-questions – Bobby Jul 22 '10 at 14:28
  • 6
    If he doesn't know what he's looking for, Googling can be hard, especially since Google doesn't even recognize the ^ character. Plus, he also wants to know "when to use it" something no place could answer better than StackOverflow – Neil N Jul 22 '10 at 14:30
  • 11
    What *I* want to know is, when do I use the ^_^ operator? – Michael Myers Jul 22 '10 at 14:32
  • 1
    @Neil N: Knowing that `^` is an operator, and that it's about the C# language, and that the language is from Microsoft, and that Microsoft's developer platform is known as MSDN, one can search for `MSDN C# operators`. That's how I ended up at the page linked to in the above comment. – stakx - no longer contributing Jul 22 '10 at 17:11

6 Answers6

9

^ is a Logical XOR Operator if the operands are bools, otherwise it's a Bitwise XOR Operator

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx

sshow
  • 8,820
  • 4
  • 51
  • 82
  • 4
    "Logical XOR operator." - That's only true if the operands are bools, otherwise it's a bitwise operator. Playing fast-and-loose with the terms 'bitwise' and 'logical' makes life very hard when you come to explain the difference between & and && in 'C'... – Will Dean Jul 22 '10 at 14:44
3

It's the XOR operator. It's used in bitwise operations, where the result is true if the left side is true or the right side is true, but false if both are true or both are false. So 0xf8 ^ 0x3f would be:

1111 1000
0011 1111
---------
1100 0111

Which is C7 in hexadecimal.

In general, if you're not doing bitwise arithmetic, you won't need to worry about it.

Chris B.
  • 85,731
  • 25
  • 98
  • 139
1

http://msdn.microsoft.com/en-us/library/zkacc7k1(VS.71).aspx

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1
  • It is often used as a way to "flip bits" by XORing it with 1 (to flip), 0 (to keep). Usually this is useful in encryption/decryption/hashing. ** THIS IS ACTUALLY USEFUL **

Example:

101 ^  
110  
-----
011   //Flip the first 2, keep the 3rd
  • It can also be used for a swapping method (though, using the standard way and generics is probably more ideal):

Example:

int myMoney = 10;
int yourMoney = 50;
Swap(myMoney, yourMoney)

public void Swap(ref int a, ref int b) //No 'temp' variable necessary
{
  a ^= b;
  b ^= a;
  a ^= b;
}
  • It is used in binary arithmetic. ** THIS IS ACTUALLY USEFUL **

  • Flip a bool (though, I'd rather use bool x = true; x != x;

Example:

public bool flip(ref bool b)
{
    b ^= true;
}
myermian
  • 31,823
  • 24
  • 123
  • 215
0

I think of it as a binary operator just like ||, &&, etc...

If I were writing logic and ended up with:

if( (condition1 && !condition2) || (condition2 && !condition1) )
{
}

I might rewrite it as:

if( condition1 ^ condition2)
{
}

That said, I'd take it on a case by base basis and weigh the benefit of brevity vs. potential obfuscation due to relative obscurity.

Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121
0

Its Exclusive OR (XOR) operator as mentioned by others. Here is the truth table for XOR

P    Q    P^Q
T    T     F
T    F     T
F    T     T
F    F     F

Note that P^Q is equal to P!=Q. Sometimes P!=Q is used in the code instead of XOR operator.

Babar
  • 2,786
  • 3
  • 25
  • 35