規則,相同則為假,不同則為真
1 ^ 1 = 0
1 ^ 0 = 1
以下為測試腳本。例子1是判斷一個數組所有的元素是否一致,例子2是異或做簡單加密的方法。
void OnEnable()
{
Example1();
Example2();
}
void Example1()
{
var a = new int[] { 6, 6, 6, 6, 6, 6 };
var b = a[0];
for (int i = 0; i < a.Length; i++)
{
b = a[i] ^ b;
}
Debug.Log(b == 0);//true
}
void Example2()
{
var a = 4;
var key = 3;
var b = a;
b = b ^ key;
//encryption.
Debug.Log((b ^ key) == a);//true
//check.
}