Section 15.39

Boolean algebra and De Morgan laws

Definition

Boolean algebra is the algebra of the two truth values, true and false, written 1 and 0, with three basic operations: AND, true only when both inputs are true; OR, true when at least one input is true; and NOT, which swaps true and false. Its laws state which expressions always have equal values. De Morgan’s laws are its two laws for negating a compound condition: NOT (XX AND YY) equals (NOT XX) OR (NOT YY), and NOT (XX OR YY) equals (NOT XX) AND (NOT YY).

The idea

An access rule says: allow the request if the user is authenticated and holds either the ordinary role or the recovery role. A programmer must also write the opposite branch, the condition for refusing. Flipping each word by feel gives “not authenticated, or not ordinary, or not recovery”. That condition refuses an authenticated user who holds only the recovery role, a user the rule allows. Boolean algebra replaces feel by calculation: the refusal condition is the negation of the allow condition, and De Morgan’s laws say how to move that negation inside the parentheses without changing what the expression means.

De Morgan’s laws match ordinary speech. “Not both” means “at least one is missing”: a door that needs a key and a badge stays shut if the key is missing or the badge is missing. “Not either” means “both are missing”: a door that opens with a key or a badge stays shut only if both are missing. Negation exchanges AND with OR and flips each condition; the grouping stays where it was. The algebra is named after George Boole and the laws after Augustus De Morgan, nineteenth-century mathematicians who wrote on logic.

How it works

Write ∧\land for AND, ∨\lor for OR and ¬\neg for NOT, with 1 for true and 0 for false. For any truth values XX, YY and ZZ, the basic laws are these.

Operator precedence is the rule for which operator applies first when there are no parentheses. In logic and in most programming languages, NOT comes first, then AND, then OR: in C, ! before && before ||; in Python §12.23, not before and before or. So a && b || c means (a && b) || c.

To negate a condition with several clauses:

  1. Write the condition with parentheses around every group, so that precedence no longer decides anything.

  2. Put NOT in front of the whole condition.

  3. Apply De Morgan’s law to the outermost operator: ¬(P∧Q)\neg(P \land Q) becomes ¬P∨¬Q\neg P \lor \neg Q, and ¬(P∨Q)\neg(P \lor Q) becomes ¬P∧¬Q\neg P \land \neg Q, where PP and QQ may themselves be groups.

  4. Repeat inside each negated group until NOT stands only in front of single conditions, and remove double negations.

  5. Keep every pair of parentheses. Each AND has become OR and each OR has become AND, but the groups are the same.

  6. Check the result with a truth table, a table that lists every combination of input values: for kk conditions it has 2k2^k rows, and the negation must give the opposite value of the original in every row.

Worked example

Let AA mean “authenticated”, BB “holds the ordinary role” and CC “holds the recovery role”. The rule is Allow=A∧(B∨C)\mathrm{Allow} = A \land (B \lor C). Negate it step by step: ¬(A∧(B∨C))=¬A∨¬(B∨C)=¬A∨(¬B∧¬C).\neg(A \land (B \lor C)) = \neg A \lor \neg(B \lor C) = \neg A \lor (\neg B \land \neg C). The first step applies De Morgan’s law for AND with X=AX = A and Y=B∨CY = B \lor C; the second applies the law for OR to the group B∨CB \lor C. So Deny=¬A∨(¬B∧¬C)\mathrm{Deny} = \neg A \lor (\neg B \land \neg C): refuse if not authenticated, or if holding neither role.

The truth table checks the result and exposes the version flipped by feel, Wrong=¬A∨¬B∨¬C\mathrm{Wrong} = \neg A \lor \neg B \lor \neg C. In the table, 1 is true and 0 is false.

AA BB CC Allow Deny Wrong
0 0 0 0 1 1
0 0 1 0 1 1
0 1 0 0 1 1
0 1 1 0 1 1
1 0 0 0 1 1
1 0 1 1 0 1
1 1 0 1 0 1
1 1 1 1 0 0

Deny is the opposite of Allow in all 8 rows. Wrong agrees with Deny in 6 rows but refuses the allowed users in the rows A=1,B=0,C=1A = 1, B = 0, C = 1 and A=1,B=1,C=0A = 1, B = 1, C = 0: an authenticated user with exactly one of the two roles. In the first of those rows, Allow=1∧(0∨1)=1∧1=1\mathrm{Allow} = 1 \land (0 \lor 1) = 1 \land 1 = 1, Deny=0∨(1∧0)=0∨0=0\mathrm{Deny} = 0 \lor (1 \land 0) = 0 \lor 0 = 0, and Wrong=0∨1∨0=1\mathrm{Wrong} = 0 \lor 1 \lor 0 = 1.

A precedence mistake goes the other way. Written without parentheses as A && B || C, the rule means (A∧B)∨C(A \land B) \lor C. In the row A=0A = 0, B=0B = 0, C=1C = 1, that gives (0∧0)∨1=1(0 \land 0) \lor 1 = 1: it allows an unauthenticated request whose recovery flag is set, where the intended rule gives 0∧(0∨1)=00 \land (0 \lor 1) = 0.

Stated precisely

For all X,Y∈{0,1}X, Y \in \{0, 1\}, ¬(X∧Y)=¬X∨¬Yand¬(X∨Y)=¬X∧¬Y.\neg(X \land Y) = \neg X \lor \neg Y \qquad\text{and}\qquad \neg(X \lor Y) = \neg X \land \neg Y. A truth table proves the first law:

XX YY ¬(X∧Y)\neg(X \land Y) ¬X∨¬Y\neg X \lor \neg Y
0 0 1 1
0 1 1 1
1 0 1 1
1 1 0 0

The second law follows from the first: apply the first to ¬X\neg X and ¬Y\neg Y, which gives ¬(¬X∧¬Y)=X∨Y\neg(\neg X \land \neg Y) = X \lor Y, and negate both sides.

For nn conditions the laws extend to ¬(X1∧…∧Xn)=¬X1∨…∨¬Xn\neg(X_1 \land \dots \land X_n) = \neg X_1 \lor \dots \lor \neg X_n and ¬(X1∨…∨Xn)=¬X1∧…∧¬Xn\neg(X_1 \lor \dots \lor X_n) = \neg X_1 \land \dots \land \neg X_n. Applied repeatedly, they give the general rule: the negation of any expression built from AND, OR and NOT is obtained by exchanging AND and OR everywhere, exchanging the constants 0 and 1, and negating every single condition, while the nesting of the groups stays unchanged.

The same laws hold for sets, with intersection for AND, union for OR and complement for NOT: the complement of the intersection of two sets is the union of their complements, and the complement of their union is the intersection of their complements.

Two assumptions carry the laws into program code. First, each condition has exactly two possible values. Second, the condition and its negation are evaluated on the same state, with no side effects and no errors during evaluation. The laws are statements about values, not about computations.

Limits and common mistakes

Short-circuit evaluation is the rule, in C, Python and most other languages, that the right operand of AND is not evaluated when the left one is false, and the right operand of OR is not evaluated when the left one is true. De Morgan’s rewriting keeps the operands in their order and so keeps this behavior: for an integer field x, both !(p != NULL && p->x > 0) and p == NULL || p->x <= 0 read p->x only when p is not a null pointer §4.26. Reordering operands, which the commutative law allows for values, can break code: p->x > 0 && p != NULL reads through a null pointer before testing it.

Negating a comparison is a separate step, and it is valid only when every pair of values is ordered. NOT (x<yx < y) equals x≥yx \ge y for integers, but not for floating-point numbers §4.49, because every ordered comparison with NaN (not a number) is false.

A bitwise operation §4.50 is not a logical operation. In C, 1 & 2 is 0 while 1 && 2 is 1, and ~1 is a non-zero value, which counts as true, while !1 is 0. A policy written with &, | and ~ on flag values that are not exactly 0 or 1 does not follow the truth tables above. In C, == also binds more tightly than &, so x & MASK == 0 means x & (MASK == 0).

Some systems use three truth values. In SQL (Structured Query Language), a comparison with the missing value NULL gives a third value, UNKNOWN. De Morgan’s laws still hold there, but NOT UNKNOWN is UNKNOWN, so a row can satisfy neither a condition nor its negation, and a query that selects rows by the negation does not return it.

A correct negation of a condition built on wrong facts is still wrong. If the authenticated flag is stale, or was never checked, the algebra cannot repair it.

Questions answered

How do you negate a multi-clause policy without changing operator precedence?

Put parentheses around every group of the policy first, then negate the whole expression and push the negation inward with De Morgan’s laws, keeping every group’s parentheses. De Morgan’s laws say that NOT (XX AND YY) equals (NOT XX) OR (NOT YY), and that NOT (XX OR YY) equals (NOT XX) AND (NOT YY). Applied group by group, they turn every AND into OR and every OR into AND and negate every single condition, while the grouping stays exactly as it was. For example, allow =A= A AND (BB OR CC) becomes deny == (NOT AA) OR ((NOT BB) AND (NOT CC)). Writing NOT AA OR NOT BB OR NOT CC instead refuses an authenticated user who holds exactly one of the two roles BB and CC, a user the policy allows.

Check the result with a truth table: the negation must be the opposite of the original for every combination of inputs, which is 2k2^k rows for kk conditions. In code, also keep the operands in their original order, so that short-circuit evaluation still lets an earlier test guard a later operand, as a null-pointer test guards a read through the pointer, and do not substitute bitwise operators for logical ones.

Related entries: Authorization (§1.7), State transition guard (§8.34), Boundary-value testing (§10.31), Bit operations: truth tables, masks, shifts, and rotations (§4.50), Binary floating-point representation, special values, and rounding (§4.49), Set inclusion and policy order (§15.40).