Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple demo for XOR
class Program
{
static void Main(string[] args)
{
int[] a1 = { 1, 1, 2 };
int[] a2 = { 17, 17, 3, 17, 17, 17, 17 };
n1 = GetStrayNumber(a1);
n2 = GetStrayNumber(a2);
WriteLine($"n1 => {n1}, n2 => {n2}");
}
private static int GetStrayNumber(int[] arr)
{
Func<int, string> format = n => $"{n} ({Convert.ToString(n, 2).PadLeft(5, '0')})";
var xor = arr[0];
for (int i = 0; i < arr.Length - 1; i++)
{
var a = xor;
var b = arr[i + 1];
xor = a ^ b;
WriteLine($"{format(a)} ^ {format(b)} = {format(xor)}");
}
WriteLine(new string('=', 80));
return xor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.