xor equivalent with AND OR logic

조회 수: 12 (최근 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 CenterFile Exchange에서 Constants and Test Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by