xor equivalent with AND OR logic
6 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
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 个评论
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’
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Constants and Test Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!