Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the ^ operator really the XOR operator in C#?

Tags:

operators

c#

xor

I read that the ^ operator is the logical XOR operator in C#, but I also thought it was the "power of" operator. What is the explanation?

like image 595
RCIX Avatar asked Oct 02 '09 07:10

RCIX


People also ask

Is XOR an operator?

XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).

Why is XOR in C?

The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different. The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

Which is the exclusive or operator in C?

The bitwise exclusive OR operator ( ^ ) compares each bit of its first operand to the corresponding bit of its second operand. If the bit in one of the operands is 0 and the bit in the other operand is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.


1 Answers

It is not the power of operator of C# since there is no such operator in C#. It is just the XOR operator.

For "power of", use Math.Pow.

As you can see from this page on the C# Operators, ^ is listed under the "Logical (boolean and bitwise)" category, which means it can both handle boolean values, and binary values (for bitwise XOR).

like image 94
Lasse V. Karlsen Avatar answered Sep 23 '22 01:09

Lasse V. Karlsen