For faster navigation, this Iframe is preloading the Wikiwand page for 逻辑异或.

逻辑异或

本條目存在以下問題,請協助改善本條目或在討論頁針對議題發表看法。 此條目没有列出任何参考或来源。 (2018年12月5日)維基百科所有的內容都應該可供查證。请协助補充可靠来源改善这篇条目。无法查证的內容可能會因為異議提出而被移除。 此條目需要精通或熟悉相关主题的编者参与及协助编辑。 (2013年10月25日)請邀請適合的人士改善本条目。更多的細節與詳情請參见討論頁
文氏图

在数字逻辑中,逻辑算符互斥或(英語:Exclusive or)是对两个运算元的一种邏輯分析类型。与一般的邏輯或不同,當兩兩數值相同時為否,而數值不同時為真。[1]

对于命题 异或通常记作。在编程语言中,常写作p ^ q

逻辑异或相当于逻辑不等价,或者说逻辑异或的逻辑非逻辑等价

真值表

[编辑]

异或运算真值表如下:

True True False
True False True
False True True
False False False
注:True:真,False:假。

无论怎样改变同一行中的位置,真值表都是成立的。

其他表示

[编辑]

在数学和工程学中,常常用其他的逻辑运算符来表示异或算符。异或算符可以使用逻辑算符逻辑与逻辑或逻辑非表示为:

另外,异或算符可以被推广,得到关于n个运算元的异或运算:n个运算元的n维异或的值为真当且仅当其中值为真的运算元有奇数个。

异或也可以被表示为:

异或还可以看作是逻辑等价关系的非运算。

性质

[编辑]

交换律

结合律

恒等律

归零律

对合运算

与抽象代數的關係

[编辑]

尽管算子逻辑合取)与逻辑析取)是逻辑系统中最为常见的算子,但结构上,系统 and 只是幺半群。因此,这两个系统无法合成为一个更大的结构,比如半环

但是,带有逻辑异或的系统是一个交换群。因此,算子的结合在集合上作用就产生了最基本的二元。这个域可以得出所有运用可以得到的结果,并且由于附带了域的结构,可以进行代数上的进一步分析。

類似符號

[编辑]
名稱 符號 Unicode 圖形 符號的來源
地球 🜨 U+2295 地球的天文符號 带有赤道和一條經線的球體

應用

[编辑]

使用异或运算交换两个 int 类型变量的数值

[编辑]

C/C++

void swap(int *a, int *b) {
    *a ^= *b;
    *b ^= *a;
    *a ^= *b;
}

Java

public void swap(int a, int b) {
    a ^= b;
    b ^= a;
    a ^= b;
}

C#

public void swap(ref int a,ref int b)
{
    a ^= b;
    b ^= a;
    a ^= b;
}

Rust

fn swap<'a, 'b>( num_a: &'a mut i32, num_b: &'b mut i32 ) {
    *num_a ^= *num_b;
    *num_b ^= *num_a;
    *num_a ^= *num_b;
}

雖然XOR運算可用來交換變數,但比起使用額外變數來交換變數的做法相比,效能反而比較差。

参考来源

[编辑]
  1. ^ Germundsson, Roger; Weisstein, Eric. XOR. MathWorld. Wolfram Research. [17 June 2015]. (原始内容存档于2015-09-05). 

参见

[编辑]
{{bottomLinkPreText}} {{bottomLinkText}}
逻辑异或
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?