Created
October 7, 2017 18:40
-
-
Save dance2die/8a9bc2ba9fa1c2e265b3e7b9a91e5412 to your computer and use it in GitHub Desktop.
Simple demo for XOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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