Home
Map
Bitwise XOR OperatorUse the bitwise XOR operator to manipulate bits in an int variable.
C#
This page was last reviewed on Jan 30, 2022.
XOR. 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.
Pattern, continued. 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.
Bitwise Or
Input and output. 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
Example. 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).
Here We use a method (GetIntBinaryString) that shows us the bits that are set in each number.
Binary Representation
And We see the bits set in the first operand, the second operand, and the result returned by the XOR operation.
Result The first 2 lines are the bits of the operands. The third line is the result from the "^" operator applied to those operands.
Tip In places where only one bit is set in both operands, the result value has that same bit set. Otherwise no bits were set.
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)); } /// <summary> /// Returns binary representation string. /// </summary> static string GetIntBinaryString(int n) { char[] b = new char[32]; int pos = 31; int i = 0; while (i < 32) { if ((n & (1 << i)) != 0) { b[pos] = '1'; } else { b[pos] = '0'; } pos--; i++; } return new string(b); } }
00000000000000000001010110101110 00000000000000000000001100100000 00000000000000000001011010001110 00000000000000000000000001100100 00000000000000000000000000100001 00000000000000000000000001000101
A discussion. 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.
Array
Info For some programs, such as those that use digital trees, you can use bitwise operators to test the bit patterns at each node.
And This yields an efficient search tree. Bitwise operators can also be used for hash code computations.
Tree
GetHashCode
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 30, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.