检查 NOT XOR 的布尔状态是否与检查等价性相同?

Is checking the boolean state of NOT XOR the same as checking equivalence?

我正在阅读一些在线编写的 C 代码并遇到以下行:

if(!(array[index] ^ array[index - 1]))

^ 运算符是按位异或,所以我读到这一行说如果 "The array value at index is not different to the value at the previous index." 它将 return 为真 简化,我读为 "If the array value at the index is the same as the one at the previous index."

当我这样读的时候,它似乎是一种过于复杂的写法:

if(array[index] == array[index - 1])

这些表达方式一样吗?如果不是,那为什么呢? 如果我没有误读它,我得到的最好解释是,因为这段代码涉及时钟信号的中断,所以它需要很快。也许按位运算比 ==?

幕后发生的任何事情都快

是的,基本上它们是一样的。

让我们看看这里:

 a    b     (a) xnor (b)
___|_____|_______________|
 0 |  0  |       1
 0 |  1  |       0
 1 |  0  |       0 
 1 |  1  |       1

正如你在这里看到的,xnor returns 只有当 a 和 b 相等时才为 1。

这是嵌入式 C 语言中最受青睐的技术之一,尤其是当您有内存或时间限制时。

自算术逻辑单元 (ALU) 起,包括一个 n 位 XNOR 电路(n:取决于您的处理器架构);与 XNOR 的比较将在一个指令周期内处理。

[如有不对请有经验的人指正]