xor equivalent with AND OR logic
Ältere Kommentare anzeigen
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?
Antworten (2)
Star Strider
am 15 Jun. 2014
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 Kommentare
Rick
am 15 Jun. 2014
Roger Stafford
am 15 Jun. 2014
The third one.
Star Strider
am 15 Jun. 2014
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
am 15 Jun. 2014
1 Stimme
Kategorien
Mehr zu Logical (Boolean) Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!