Skip to main content

Logic Gates · 40 min

Boolean Algebra

The algebra of true and false: operators, identities, normal forms, Karnaugh maps, and the minimization step used before building gates.

Why This Matters

A one-bit full adder has three inputs and two outputs. Its carry output is ab+ac+bcab+ac+bc. Written without Boolean algebra, the same circuit might be drawn from a full truth table with four separate product terms, wasting gates and wire delay.

Chip design tools still start from this algebra. A compiler, hardware description language, or hand schematic turns predicates into Boolean functions f:{0,1}n{0,1}f:\lbrace 0,1\rbrace^n \to \lbrace 0,1\rbrace, rewrites them using identities, then maps the result to NAND, NOR, multiplexers, lookup tables, or standard cells.

Core Definitions

Definition

Boolean value

A Boolean value is one of two symbols, usually written 00 and 11. In circuits, 00 and 11 denote voltage ranges, not exact voltages. In logic, they denote false and true.

Definition

Boolean function

An nn-input Boolean function is a map f:{0,1}n{0,1}f:\lbrace 0,1\rbrace^n \to \lbrace 0,1\rbrace. The function is completely specified by its truth table with 2n2^n rows.

Definition

Literal, minterm, maxterm

A literal is a variable xx or its complement xˉ\bar x. A minterm is an AND of one literal for each variable. A maxterm is an OR of one literal for each variable.

Definition

Canonical normal form

Canonical sum-of-products lists every input row where the function is 11. Canonical product-of-sums lists every input row where the function is 00.

Operators and Truth Tables

The four operators used constantly are AND, OR, NOT, and XOR. We write AND by juxtaposition or \cdot, OR by ++, NOT by a bar, and XOR by \oplus.

aabbababa+ba+baba \oplus b
00000
01011
10011
11110

XOR differs from OR in exactly one row. For a=b=1a=b=1, OR returns 11 while XOR returns 00. This is the source of many bugs when a bit mask operation is mistaken for a logical condition.

A truth table for three inputs has eight rows. The majority-of-three function returns 11 when at least two inputs are 11.

aabbccmajority
0000
0010
0100
0111
1000
1011
1101
1111

The canonical SOP is f=aˉbc+abˉc+abcˉ+abcf=\bar a b c+a \bar b c+a b \bar c+a b c. Boolean algebra reduces it to f=ab+ac+bcf=ab+ac+bc, the carry output of a full adder.

The same operators act on all bits of a machine word in C. This example uses one byte so the layout fits on one line.

#include <stdint.h>
#include <stdio.h>

int main(void) {
    uint8_t x = 0b10110100;
    uint8_t mask = 0b00001111;

    uint8_t low  = x & mask;   // 00000100
    uint8_t set  = x | mask;   // 10111111
    uint8_t flip = x ^ mask;   // 10111011
    uint8_t inv  = (uint8_t)~x; // 01001011

    printf("%02x %02x %02x %02x\n", low, set, flip, inv);
}

Byte layout for the first operation:

x       10110100
mask    00001111
x&mask  00000100

Each output bit is computed independently. Bit 2 survives because both inputs have a 11 there; bit 7 becomes 00 because the mask has a 00.

Algebraic Identities

Boolean algebra has identities similar to ordinary algebra, plus identities that depend on two-valued logic.

NameIdentity
Idempotencex+x=xx+x=x and xx=xxx=x
Commutativityx+y=y+xx+y=y+x and xy=yxxy=yx
Associativity(x+y)+z=x+(y+z)(x+y)+z=x+(y+z) and (xy)z=x(yz)(xy)z=x(yz)
Distributivityx(y+z)=xy+xzx(y+z)=xy+xz and x+yz=(x+y)(x+z)x+yz=(x+y)(x+z)
Complementx+xˉ=1x+\bar x=1 and xxˉ=0x\bar x=0
Identityx+0=xx+0=x and x1=xx1=x
Dominationx+1=1x+1=1 and x0=0x0=0
Absorptionx+xy=xx+xy=x and x(x+y)=xx(x+y)=x

The second distributive law, x+yz=(x+y)(x+z)x+yz=(x+y)(x+z), is valid in Boolean algebra but has no direct analogue in ordinary arithmetic. Verify it by cases. If x=1x=1, both sides equal 11. If x=0x=0, both sides equal yzyz.

Absorption is the standard cleanup after expanding a circuit expression. Starting from a+aba+ab, factor a(1+b)a(1+b). Since 1+b=11+b=1, the expression is aa. In a circuit this removes the AND gate and the OR gate; the output wire is just aa.

A small instruction-selection example shows the same algebra in software. Suppose an execution unit should start when valid is true and either is_add or is_sub is true.

uint8_t issue(uint8_t valid, uint8_t is_add, uint8_t is_sub) {
    return valid & (is_add | is_sub);
}

If the decoder guarantees is_add | is_sub only when valid is already true, then valid & (is_add | is_sub) absorbs to is_add | is_sub. That rewrite is legal only with the stated invariant.

De Morgan's Laws and Gate Rewrites

De Morgan's laws move negation across AND and OR.

xy=xˉ+yˉ\overline{xy}=\bar x+\bar y

x+y=xˉyˉ\overline{x+y}=\bar x\bar y

They matter because real gate libraries often prefer NAND and NOR gates. A NAND is xy\overline{xy}. A NOR is x+y\overline{x+y}. By pushing bubbles through a schematic, a designer can replace mixed NOT, AND, and OR gates with a small number of NAND or NOR gates.

Consider this expression:

g=a(b+c)g=\overline{a(b+c)}

Apply De Morgan twice.

g=aˉ+b+cg=\bar a+\overline{b+c}

g=aˉ+bˉcˉg=\bar a+\bar b\bar c

The original circuit has an OR for b+cb+c, an AND with aa, and a final inverter. The rewritten form has three inverters, one AND, and one OR. That is not always fewer gates. But if the target library has cheap NAND gates, implement the original as one OR feeding one NAND, or rewrite the OR itself with NAND gates.

A NAND-only implementation of b+cb+c follows from De Morgan:

b+c=bˉcˉb+c=\overline{\bar b\bar c}

Using NAND, bˉ=NAND(b,b)\bar b=\operatorname{NAND}(b,b) and cˉ=NAND(c,c)\bar c=\operatorname{NAND}(c,c). Then b+c=NAND(bˉ,cˉ)b+c=\operatorname{NAND}(\bar b,\bar c).

SOP, POS, and Karnaugh Maps

Canonical SOP is formed from rows where f=1f=1. For variables a,b,ca,b,c, row 101101 contributes abˉca\bar b c. If ff is 11 on rows 3,5,6,73,5,6,7, then

f=Σm(3,5,6,7)f=\Sigma m(3,5,6,7)

f=aˉbc+abˉc+abcˉ+abcf=\bar a b c+a\bar b c+ab\bar c+abc

After simplification, this becomes ab+ac+bcab+ac+bc.

Canonical POS is formed from rows where f=0f=0. A maxterm is false on exactly one row. For row a=1,b=0,c=1a=1,b=0,c=1, the maxterm is aˉ+b+cˉ\bar a+b+\bar c. The variable is complemented when the row value is 11, because the OR term must evaluate to 00 on that row.

For majority-of-three, the zero rows are 0,1,2,40,1,2,4.

f=ΠM(0,1,2,4)f=\Pi M(0,1,2,4)

f=(a+b+c)(a+b+cˉ)(a+bˉ+c)(aˉ+b+c)f=(a+b+c)(a+b+\bar c)(a+\bar b+c)(\bar a+b+c)

A Karnaugh map arranges truth-table cells so adjacent cells differ in one variable. For four variables, use Gray-code order 00,01,11,1000,01,11,10 on both axes. Let f(A,B,C,D)=1f(A,B,C,D)=1 on minterms 0,2,5,7,8,10,13,150,2,5,7,8,10,13,15.

AB\CDAB \backslash CD00011110
001001
010110
110110
101001

The four corner cells form one wraparound group. They share B=0B=0 and D=0D=0, so the term is BˉDˉ\bar B\bar D. The central 2×22 \times 2 group shares B=1B=1 and D=1D=1, so the term is BDBD. Thus f=BˉDˉ+BDf=\bar B\bar D+BD, which is BB XNOR DD. Inputs AA and CC vanished.

Algorithmic Minimization

Karnaugh maps work well for three or four variables. Five variables are possible but error-prone. More inputs need algorithms.

Quine-McCluskey groups minterms by the number of 11 bits, combines terms that differ in one position, and repeats until no more combinations exist. A dash means the variable disappeared.

m0  0000
m2  0010  combine with m0  00-0
m8  1000  combine with m0  -000
m10 1010  combine with m2  -010

00-0 and 10-0 combine to -0-0

The implicant -0-0 means B=0,D=0B=0,D=0, so it represents BˉDˉ\bar B\bar D. Repeating the process for minterms 5,7,13,155,7,13,15 gives -1-1, which represents BDBD.

Quine-McCluskey is exact, but the prime implicant chart can grow fast. Espresso, introduced by Brayton and coauthors, is a heuristic minimizer for two-level logic. It does not promise the smallest expression for every input, but it handles cases that are too large for hand maps. Binary decision diagrams are another representation of Boolean functions, but they are outside this page.

Main Theorem

Theorem

Canonical Boolean Normal Forms

Statement

Every Boolean function f:{0,1}n{0,1}f:\lbrace 0,1\rbrace^n \to \lbrace 0,1\rbrace has a canonical sum-of-products expression and a canonical product-of-sums expression.

Intuition

A truth table lists every input row. For each row where f=1f=1, build a minterm that fires only on that row. OR those minterms. For each row where f=0f=0, build a maxterm that fails only on that row. AND those maxterms.

Proof Sketch

For an input row rr, define a minterm mrm_r by using xix_i when ri=1r_i=1 and xˉi\bar x_i when ri=0r_i=0. This minterm evaluates to 11 exactly on row rr. The OR of all mrm_r with f(r)=1f(r)=1 matches ff on every row. For POS, define a maxterm MrM_r by using xˉi\bar x_i when ri=1r_i=1 and xix_i when ri=0r_i=0. This maxterm evaluates to 00 exactly on row rr. The AND of all MrM_r with f(r)=0f(r)=0 matches ff on every row.

Why It Matters

The theorem gives a mechanical path from specification to gates. It is not the final circuit. It is the starting expression that minimization rewrites.

Failure Mode

The canonical forms have up to 2n2^n terms. For n=20n=20, the truth table has 1,048,5761,048,576 rows, so direct canonical construction is often impossible in a real design flow.

Common Confusions

Watch Out

OR is not XOR

Inclusive OR returns 11 on input 1111. XOR returns 00 on input 1111. In an adder, the sum bit uses XOR, abcina \oplus b \oplus c_{in}, while the carry bit uses majority logic.

Watch Out

A Karnaugh map wraps at the edges

The left and right columns are adjacent because Gray-code order makes 0000 and 1010 differ in one bit. The top and bottom rows are also adjacent. Missing wraparound groups is a common reason for a non-minimal expression.

Watch Out

Algebraic simplification needs the same domain

The identity x+x=xx+x=x is Boolean, not integer arithmetic. In C, x | x == x for bitwise OR, but x + x == x is false for most integers.

Exercises

ExerciseCore

Problem

For f(a,b,c)=Σm(1,3,5,7)f(a,b,c)=\Sigma m(1,3,5,7), write the canonical SOP and minimize it.

ExerciseCore

Problem

Use De Morgan's laws to rewrite (a+b)(c+d)\overline{(a+b)(c+d)} as a sum of products.

ExerciseAdvanced

Problem

Run one Quine-McCluskey combination pass for minterms 0,1,4,5,12,130,1,4,5,12,13 of variables A,B,C,DA,B,C,D. Give the minimized expression.

References

Canonical:

  • Charles Petzold, Code: The Hidden Language of Computer Hardware and Software, 2nd ed. (2022), ch. 10-14, relays, gates, binary addition, and feedback
  • David A. Patterson and John L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 5th ed. (2013), Appendix B, logic design basics and combinational circuits
  • M. Morris Mano and Michael D. Ciletti, Digital Design, 5th ed. (2013), ch. 2-3, Boolean algebra, gates, minimization, and combinational logic
  • Stephen Brown and Zvonko Vranesic, Fundamentals of Digital Logic with VHDL Design, 3rd ed. (2009), ch. 2-4, Boolean functions, Karnaugh maps, and circuit implementation
  • J. Clark Scott, But How Do It Know? The Basic Principles of Computers for Everyone (2009), ch. 1-6, gates, memory, and the path from switches to a computer

Accessible:

  • Randy H. Katz and Gaetano Borriello, Contemporary Logic Design, 2nd ed., ch. 2, Boolean algebra and two-level logic
  • R. K. Brayton, G. D. Hachtel, C. T. McMullen, and A. L. Sangiovanni-Vincentelli, Logic Minimization Algorithms for VLSI Synthesis (1984), ch. 2-4, exact and heuristic two-level minimization
  • UC Berkeley CS61C notes on synchronous digital systems and combinational logic

Next Topics

  • /computationpath/logic-gates
  • /computationpath/nand-universality
  • /computationpath/combinational-circuits
  • /topics/propositional-logic
  • /topics/sat