Exclusive-or modifies a bit pattern. If both operands have a bit set, the result does not have that bit set. If neither has a bit set, the result also does not have that bit set.
If one operand but not the other has the bit set, the result is set. So to restate, one but not both operands will result in a 1 bit.
Two numbers (operands) are passed to the XOR binary operator. And the result is a third number, containing the appropriate bits set to 1 and 0.
Operand: 100 Operand: 001 Result: 101
The XOR operator is a binary operator—it requires 2 operands. An operand is a numeric argument to the operator (the numbers on each side of the "^" caret symbol).
GetIntBinaryString
) that shows us the bits that are set in each number.using System; class Program { static void Main() { // Demonstrate XOR for two integers. int a = 5550 ^ 800; Console.WriteLine(GetIntBinaryString(5550)); Console.WriteLine(GetIntBinaryString(800)); Console.WriteLine(GetIntBinaryString(a)); Console.WriteLine(); // Repeat. int b = 100 ^ 33; Console.WriteLine(GetIntBinaryString(100)); Console.WriteLine(GetIntBinaryString(33)); Console.WriteLine(GetIntBinaryString(b)); } static string GetIntBinaryString(int value) { return Convert.ToString(value, 2).PadLeft(32, '0'); } }00000000000000000001010110101110 00000000000000000000001100100000 00000000000000000001011010001110 00000000000000000000000001100100 00000000000000000000000000100001 00000000000000000000000001000101
Bitwise operators are useful in some cases. Typically, if you don't have limited memory on your system, you can replace bitwise operations with arrays.
The XOR binary operator receives 2 operands. It returns a number that contains a bit set for each exclusive bit set. Often this operator is more trouble than it is worth.