C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: In this program, we use a method that shows us the bits that are set in each number.
And: We see the bits set in the first operand, the second operand, and the result returned by the XOR operation.
Output: The first two 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.
C# program that uses XOR operator
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);
}
}
Output
00000000000000000001010110101110
00000000000000000000001100100000
00000000000000000001011010001110
00000000000000000000000001100100
00000000000000000000000000100001
00000000000000000000000001000101
Warning: Please proceed with the XOR operator with caution. It is sometimes helpful but often not.
Optimization