c# - 什么是 C# exclusive 或 `^` 用法?

标签 c# operators bitwise-operators bitwise-xor

<分区>

任何人都可以用一个很好的例子来解释这个运算符吗?

我知道这个运算符是什么。我的意思是一个现实生活中的例子。

最佳答案

它是逻辑操作exclusive disjunction的实现

http://en.wikipedia.org/wiki/Exclusive_or

Exclusive disjunction is often used for bitwise operations. Examples:

  • 1 xor 1 = 0
  • 1 xor 0 = 1
  • 0 xor 1 = 1
  • 0 xor 0 = 0
  • 1110 xor 1001 = 0111 (this is equivalent to addition without carry)

As noted above, since exclusive disjunction is identical to addition modulo 2, the bitwise exclusive disjunction of two n-bit strings is identical to the standard vector of addition in the vector space (Z/2Z)^4.

In computer science, exclusive disjunction has several uses:

  • It tells whether two bits are unequal.
  • It is an optional bit-flipper (the deciding input chooses whether to invert the data input).
  • It tells whether there is an odd number of 1 bits ( is true iff an odd number of the variables are true).

(以及一大堆其他用途)

关于c# - 什么是 C# exclusive 或 `^` 用法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6422329/

相关文章:

ruby - 为什么可以写or却不能写||?

c - 收集位的最快方法(类似于 std::copy_if)

javascript:将二进制字符串转换为 UInt32 大端字节序而不使用按位运算

c# - 将 Windows Phone 8 应用程序设置为文件扩展名的默认值

python - 在python中可能有多个复合赋值

c# - 用正则表达式分割

python - 运算符和方法的区别

带数字的 Java 运算符

c# - 为什么在运行动态编译的 EXE 时会看到控制台窗口?

c# - 我可以在不写入中间临时存储的情况下获取文件的 GZipStream 吗?