De Morgan's Laws


Link to this posting

Postby Ursego » 21 Feb 2013, 22:20

Simplify Boolean expressions using the De Morgan's Laws:

(NOT P) OR (NOT Q) can be expressed shorter as NOT (P AND Q)
(NOT P) AND (NOT Q) can be expressed shorter as NOT (P OR Q)


So, in that situation, use the NOT operator only once, as shown below.

EXAMPLE 1 - "AND" BECOMES "OR"

The expression
PB:
Code: Select all
lb_is_gas = (not lb_is_liquid) and (not lb_is_solid) and (not lb_is_plasma)

C#:
Code: Select all
isGas = (!isLiquid) && (!isSolid) && (!isPlasma);

should be simplified to

PB:
Code: Select all
lb_is_gas = not (lb_is_liquid or lb_is_solid or lb_is_plasma)

C#:
Code: Select all
isGas = !(isLiquid || isSolid || isPlasma);

EXAMPLE 2 - "OR" BECOMES "AND"

(To go to a restaurant, 2 conditions must be satisfied: "we are hungry" AND "restaurants are open now"):

The expression
PB:
Code: Select all
lb_we_will_stay_home = (not lb_we_are_hungry) or (not lb_restaurants_are_open_now)

C#:
Code: Select all
weWillStayHome = (!weAreHungry) || (!restaurantsAreOpenNow);

should be simplified to

PB:
Code: Select all
lb_we_will_stay_home = not (lb_we_are_hungry and lb_restaurants_are_open_now)

C#:
Code: Select all
weWillStayHome = !(weAreHungry && restaurantsAreOpenNow);
User avatar
Ursego
Site Admin
 
Posts: 113
Joined: 19 Feb 2013, 20:33

IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:




free counters

eXTReMe Tracker