Digital Systems 3 min read

Boolean algebra

Boolean algebra is arithmetic on 0 and 1 with AND, OR and NOT. A few laws let you shrink a circuit or a condition without changing what it does, and De Morgan's rule is the one you will use most in code.

Left, simplifying: Y = A·B + A·B' becomes A·(B + B') by factoring, then A·1 by the complement law, then A. Right, De Morgan: NOT (A AND B) equals NOT A OR NOT B.
Simplifying A·B + A·B' step by step to just A, and De Morgan's rule for NOT.

The notation

Write AND as a dot (A·B), OR as a plus (A + B) and NOT as an apostrophe (A'). Every variable is 0 or 1, so each law can be proved by trying all the input combinations in a truth table.

The laws that matter

LawAND formOR form
IdentityA·1 = AA + 0 = A
NullA·0 = 0A + 1 = 1
ComplementA·A' = 0A + A' = 1
AbsorptionA·(A + B) = AA + A·B = A
De Morgan(A·B)' = A' + B'(A + B)' = A'·B'

AND and OR also commute and distribute, much like multiplication and addition, with one twist: OR distributes over AND too.

Simplifying a circuit

Take Y = A·B + A·B'. Factor out A to get A·(B + B'). The complement law says B + B' = 1, and the identity law says A·1 = A. So Y = A: the output ignores B entirely. Two AND gates, a NOT and an OR collapse into a wire. Fewer gates mean less area, less power and less delay.

De Morgan in your code

De Morgan's laws say that inverting an AND turns it into an OR of the inverted inputs, and the other way round. In C, !(a && b) is the same as !a || !b, and !(a || b) is the same as !a && !b. Use it to flip a condition that reads awkwardly, for example when a pin is active low:

// "not (ready and enabled)" ...
if (!(ready && enabled)) { wait(); }
// ... is the same as "not ready, or not enabled"
if (!ready || !enabled) { wait(); }

Try every combination of two inputs to convince yourself; a four-row truth table settles any doubt.