[英]What is the use of ^= and what is the opposite of it c#
大家好,我想知道在C#中該符號“ ^”是什么意思
對於這個簡單的代碼
public static Byte[] Xor = new Byte[] {0x77, 0xE8, 0x5E, 0xEC, 0xB7};
public static Byte[] data = new Byte[5];
static byte[] convertsomething(){
Byte xors = 0;
for (int i = 0; i < 100; i++)
{
data[i] ^= Xor[xors];
xors++;
}
return data;
}
對於C#代碼,我們擁有可變數據,該數據如何將其轉換回主值,或者該操作數據的反義之處[i] ^ = Xor [xors];
在C#中, ^運算符是布爾邏輯運算符 ,其目的是執行互斥或(XOR)操作。
對於布爾操作數,僅當兩個操作數之一為true時,此操作的結果才返回true,這意味着:
true ^ true // Will return false
false ^ false // Will return false
true ^ false // Will return true
對於整數操作數,它執行按位異或的示例(在括號中顯示二進制表示形式):
1 (1) ^ 1 (1) // Will return 0 (0)
0 (0) ^ 0 (0) // Will return 0 (0)
1 (1) ^ 0 (0) // Will return 1 (1)
2 (10) ^ 1 (1) // Will return 3 (11)
15 (1111) ^ 5 (101) // Will return 10 (1010)
^ =運算符將對左右操作數執行相同的操作,這意味着x ^ = y與x = x ^ y相同。
XOR的真值表可以幫助您理解:
A B Result
0 0 0
0 1 1
1 0 1
1 1 0
它的二進制運算符Binary ^運算符為整數類型和布爾值預定義
x ^= y
評估為x = x ^ y
基本上是^運算符允許的
// When one operand is true and the other is false, exclusive-OR
// returns True.
Console.WriteLine(true ^ false);
// When both operands are false, exclusive-OR returns False.
Console.WriteLine(false ^ false);
// When both operands are true, exclusive-OR returns False.
Console.WriteLine(true ^ true);
有關運算符的更多信息@ https://msdn.microsoft.com/en-us/library/0zbsw2z6.aspx和https://msdn.microsoft.com/en-us/library/zkacc7k1.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.