xor equivalent with AND OR logic

8 ビュー (過去 30 日間)
Rick
Rick 2014 年 6 月 15 日
コメント済み: Star Strider 2014 年 6 月 15 日
Which of the following expressions is equivalent to the following function? xor(A,B)
(A & B) & (A | B)
(A | B) | ~(A & B)
(A | B) & ~(A & B)
(A | B) | (A & B)
I don't understand exactly what these options are doing. I know xor gives a 0 if both statements are true or false, and gives a 1 if one statement is true and one statement is false. But how does that translate to one of these options symbolically?

回答 (2 件)

Star Strider
Star Strider 2014 年 6 月 15 日
Actually, the correct expression is much simpler:
TF = A ~= B
The ‘XOR’ operation is best described as ‘one or the other but not both’. This holds if, in this instance, A is not equal to B. The tilde ‘~’ in MATLAB is the logical negative operator, so prefacing the equal sign with it turns the expression into ‘not equal’.
Using the ‘ne’ (not equal) function has the same effect:
TF = ne(A,B)
  3 件のコメント
Roger Stafford
Roger Stafford 2014 年 6 月 15 日
The third one.
Star Strider
Star Strider 2014 年 6 月 15 日
I created this code snippet to test them:
A = 0;
B = 1;
C1 = (A & B) & (A | B);
C2 = (A | B) | ~(A & B);
C3 = (A | B) & ~(A & B);
C4 = (A | B) | (A & B);
TF1 = A ~= B;
TF2 = ne(A,B);
fprintf(1,'\n\tA = %d \tB = %d \n\t\t\tC1 = %d \tC2 = %d \tC3 = %d \tC4 = %d \tTF = %d\n', A, B, C1, C2, C3, C4, TF1)
I varied A and B between runs:
A = 0 B = 0
C1 = 0 C2 = 1 C3 = 0 C4 = 0 TF = 0
A = 1 B = 0
C1 = 0 C2 = 1 C3 = 1 C4 = 1 TF = 1
A = 1 B = 1
C1 = 1 C2 = 1 C3 = 0 C4 = 1 TF = 0
A = 0 B = 1
C1 = 0 C2 = 1 C3 = 1 C4 = 1 TF = 1
The only condition that exactly matches my ‘TF’ condition is ‘C3’, so it is equivalent to ‘XOR’

サインインしてコメントする。


per isakson
per isakson 2014 年 6 月 15 日

カテゴリ

Help Center および File ExchangeConstants and Test Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by