Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dance2die
Created October 7, 2017 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dance2die/8a9bc2ba9fa1c2e265b3e7b9a91e5412 to your computer and use it in GitHub Desktop.
Save dance2die/8a9bc2ba9fa1c2e265b3e7b9a91e5412 to your computer and use it in GitHub Desktop.
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