开发者

Logic: is ( A && !(B || C)) || ( B || C ) the same as ( A || B || C )?

I've encountered some obj-c code and I'm wondering if there's a way to simplify it:

#if ( A &&开发者_如何学编程 !(B || C)) || ( B || C )

is this the same as?

#if ( A || B || C )

If not, is there another way to formulate it that would be easier to read?

[edit] I tried the truth table before asking the question, but thought I had to be missing something because I doubted that Foundation.framework/Foundation.h would employ this more complex form. Is there a good reason for it?

Here's the original code (from Foundation.h):

#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)


Yes. Like others said, you can truth table it. The De Morgan rules can also help.

However, I think the best option is to use a Karnaugh Map. It takes a few minutes to learn, but Karnaugh Maps allow you to consistently find the most minimal expression for boolean logic. Truth tables can verify a minimization, but they can't give it to you.

Here's how I got it:

First, the table layout:

         AB
     00   01   11   10
  0|    |    |    |    |
C 1|    |    |    |    |

Now, considering your equation, B || C will always cause a truth:

         AB
     00   01   11   10
  0|    |  T |  T |    |
C 1|  T |  T |  T |  T |

This leaves only two cases. In either case, the right side evaluates to false. For 000, the left side also evaluates to false (0 && !(whatever) is false). For 100, 1 && !(0 ||| 0) evaluates to true. Thus, the statement is true. Filling in:

         AB
     00   01   11   10
  0|  F |  T |  T |  T |
C 1|  T |  T |  T |  T |

Now, we only need to "cover" all the truths. "C" will cover the bottom row. "B" will cover the middle square (of four values). Thus, "B || C" covers all but the top right square. Now, "A" will cover the right four-space square. It's OK that this is redundant. Thus, "A || B || C" covers all the true squares and omits the only false one.


Get pen + paper + try it, there are only 8 possible inputs


They are the same. You can use Truth Table Generator to test it. Both these expressions give false only in one case, when A, B and C are false.


A | B | C | (B || C) | (!(B || C)) | (A && !(B || C)) | (A && (!(B || C)) || (B || C) | (A || B || C)
------------------------------------------------------------------------------------------------------
T | T | T |     T    |       F     |         F        |                 T             |         T      
T | T | F |     T    |       F     |         F        |                 T             |         T 
T | F | T |     T    |       F     |         F        |                 T             |         T 
T | F | F |     F    |       T     |         T        |                 T             |         T 
F | T | T |     T    |       F     |         F        |                 T             |         T 
F | T | F |     T    |       F     |         F        |                 T             |         T 
F | F | T |     T    |       F     |         F        |                 T             |         T 
F | F | F |     F    |       T     |         F        |                 F             |         F 

Based on the last two columns, I would say yes.


Yes it is the same. Using De Morgan rules:

(A && !(B || C)) || (B || C) = (A && !B && !C) || (B || C). So the second will be true when A = 1 and B, C = 0. If that is not the case the second part (B || C) will be true when B || C. So it is equal to the first.


You could also say :

(A && !(B || C)) || (B || C) rewrites to (A && !W) || W (1)

(1) rewrites to (A && !W) || (A || !A || W) (2)

(2) rewrites (A && !W) || (A || W) || (!A || W) (3)

(3) rewrites (A && !W) || !(A && !W) || (A || W) (4)

(4) leads to A || W and then A || B || C


Yes, the two expressions are equivalent. (I just wrote a couple of functions to test all eight possibilities.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜