Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use "^" operator

Tags:

c#

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

My programing language is C#.

like image 645
Gaby Avatar asked Jul 22 '10 14:07

Gaby


People also ask

What is the use of +operator?

Addition (+) This operator provides the sums of two numbers. Basic arithmetic operator used for addition; the result of an arithmetic operator is usually a numeric value.

Where do we use and operators?

Two or more relations can be logically joined using the logical operators AND and OR . Logical operators combine relations according to the following rules: The ampersand (&) symbol is a valid substitute for the logical operator AND . The vertical bar ( | ) is a valid substitute for the logical operator OR .

Why do people use or operator?

The OR operator is used in most programming languages which support logical and comparison operators. In the programming world, it is mainly used to control the flow in programs, similar to other logical operators. It is also an important component while setting up digital circuit logic.

What is the use of operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.


2 Answers

^ 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

like image 143
sshow Avatar answered Oct 27 '22 00:10

sshow


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.

like image 43
Chris B. Avatar answered Oct 26 '22 22:10

Chris B.