# Boolean Variables and Logic Gates

Computer Science I · Basic Digital Design · https://tryals.app/learn/computer-science-i/boolean-variables-and-logic-gates

## The Alphabet of Hardware

Digital circuits compute with just two voltage levels, interpreted as **boolean** values **1** (true, high) and **0** (false, low). Every digital operation is built from **logic gates**, hardware elements computing a boolean function.

| Gate | Output is 1 when... |
| --- | --- |
| **AND** | all inputs are 1 |
| **OR** | at least one input is 1 |
| **NOT** | the single input is 0 |
| **NAND** | inverse of AND (0 only if all are 1) |
| **NOR** | inverse of OR (1 only if all are 0) |
| **XOR** | the inputs differ |

**NAND is universal**: any boolean function can be built using only NAND gates (NOR is also universal). Gates are physical atoms; everything from an adder to a processor is built by wiring them together.

## OR vs. XOR & Formula

**Common pitfall:** confusing **OR** with **XOR**. **OR** outputs 1 when _at least one_ input is 1, including when _both_ are 1. **XOR** (exclusive-OR) outputs 1 only when inputs _differ_ ($A \neq B$).

For inputs (1, 1), **OR** glows (1) but **XOR** stays dark (0). Watch this "both are 1" edge case where they disagree.

The formal definition for **XOR** is:

$$A \oplus B = 1 \iff A \neq B$$

where $A$ and $B$ are boolean variables, $\oplus$ denotes XOR, and $\iff$ means "if and only if."

## Practice questions

2 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. A CMOS chip is built almost entirely from NAND and NOR gates, and AND is constructed as NAND followed by an inverter. Why is the 'simpler' gate the more expensive one?

A. Because AND gates switch more slowly than NAND at every process node
B. Because designers prefer uniformity, and the transistor cost is identical
C. Because CMOS transistors naturally produce inverting logic
D. Because AND gates are patented and NAND gates are not

**Answer:** C. Because CMOS transistors naturally produce inverting logic

**Why:** The physics is inverting, so NAND and NOR are the cheap primitives and the intuitive gates are the composites. AND costs six transistors to NAND's four. It is a clean case of the implementation layer disagreeing with the conceptual one, and the manufacturing follows the physics.

Page: https://tryals.app/practice/computer-science-i/boolean-variables-and-logic-gates/a-cmos-chip-is-built-almost-entirely-from-nand-and-nor-gates-and-and

### 2. NAND is called universal because every other logic gate can be built from NAND gates alone.

**Answer:** True

**Why:** True. Tying a NAND's inputs together gives NOT; NAND followed by NOT gives AND; De Morgan gives OR from NANDs with inverted inputs. Since {AND, OR, NOT} is functionally complete, so is NAND alone, which is why fabs optimise one gate type and build whole chips from it.

Page: https://tryals.app/practice/computer-science-i/boolean-variables-and-logic-gates/nand-is-called-universal-because-every-other-logic-gate-can-be-built
