Sub topic 1
Boolean values and operators
Prerequisites
This chapter assumes you already know:
- Binary numbers and how to convert between binary and decimal.
- What the basic gates (AND, OR, NOT, NAND, NOR, XOR, XNOR) look like and what they do.
- Basic propositional logic notation, such as ∧, ∨ and ¬.
Here you will fix the notation for Boolean algebra and the exact meaning and precedence of each operator. You will also learn the XOR properties that GATE questions rely on. The later chapters on truth tables, laws and minimization build on all of this.
Boolean values
A Boolean algebra for digital logic has two values, written 0 and 1. In hardware, 0 usually means a low voltage and 1 a high voltage. In logic, 0 means false and 1 means true.
A Boolean variable holds one of these two values. A Boolean function maps each assignment of its n input variables to 0 or 1. There are 2^n possible assignments of n variables, so a function has 2^n rows when you tabulate it. Each row can independently be 0 or 1, so there are 2^(2^n) distinct functions of n variables:
| Variables n | Input rows 2^n | Distinct functions 2^(2^n) |
|---|---|---|
| 1 | 2 | 4 |
| 2 | 4 | 16 |
| 3 | 8 | 256 |
| 4 | 16 | 65536 |
GATE sometimes asks for this count directly. For example, the number of distinct functions of 3 variables is 256.
The core operators
Three operators define the algebra. A fourth, XOR, appears so often that it is treated as a basic operator.
| Operator | Common notations | Result is 1 when |
|---|---|---|
| NOT | A', ¬A, Ā | A is 0 |
| AND | A·B, AB, A ∧ B | both A and B are 1 |
| OR | A + B, A ∨ B | at least one of A, B is 1 |
| XOR | A ⊕ B | A and B differ |
The full truth table for two inputs is:
| A | B | A·B | A + B | A ⊕ B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Be careful with the symbol +. In Boolean algebra it means OR, not addition, so 1 + 1 = 1. Writing AB with no symbol means AND, just as in ordinary algebra.
The derived operators are:
- NAND: (A·B)', which is 0 only when both inputs are 1.
- NOR: (A + B)', which is 1 only when both inputs are 0.
- XNOR: (A ⊕ B)', which is 1 when the inputs are equal.
Operator precedence
Without parentheses, evaluate in this order:
- NOT (highest)
- AND
- OR (lowest)
So A + B·C' means A + (B·(C')). It does not mean (A + B)·C'. Writing a product next to a sum is the usual source of mistakes in hand evaluation.
Textbooks do not agree on where XOR sits. Many place it at the same level as OR or between AND and OR. If an expression mixes ⊕ with + or ·, add parentheses before you do anything else. In an exam, assume the question intends the grouping it shows and do not guess at precedence.
XOR in depth
XOR is the operator most likely to be mishandled. These facts are worth memorizing:
- A ⊕ 0 = A, so 0 is the identity.
- A ⊕ 1 = A', so XOR with 1 inverts.
- A ⊕ A = 0.
- A ⊕ A' = 1.
- XOR is commutative and associative, so the grouping and order of a chain A ⊕ B ⊕ C do not matter.
- AND distributes over XOR: A·(B ⊕ C) = A·B ⊕ A·C.
- OR does not distribute over XOR in the same way.
Written with AND, OR and NOT, the two-input forms are:
- A ⊕ B = A'B + AB'
- (A ⊕ B)' = AB + A'B'
For a chain of n variables, A1 ⊕ A2 ⊕ ... ⊕ An equals 1 exactly when an odd number of the inputs are 1. This is why XOR computes parity. The chain's truth table has 2^(n-1) ones, which is half the rows. Each added variable keeps this half-and-half pattern, and that is why XOR functions cannot be reduced with a K-map: the ones sit on a checkerboard, so no two are adjacent.
Operators in code
Python's bitwise operators match the Boolean operators when the values are the integers 0 and 1. The one trap is NOT. The operator ~ inverts every bit of a Python integer, so ~1 is -2, not 0. To invert a single Boolean bit, XOR it with 1.
from itertools import product
def NOT(a):
return a ^ 1
def AND(a, b):
return a & b
def OR(a, b):
return a | b
def XOR(a, b):
return a ^ b
print("A B | AND OR XOR")
for a, b in product([0, 1], repeat=2):
print(a, b, "|", AND(a, b), " ", OR(a, b), " ", XOR(a, b))
print("~1 =", ~1, " NOT(1) =", NOT(1))
# F = A + B·C' evaluated at A=0, B=1, C=0
A, B, C = 0, 1, 0
print("F =", OR(A, AND(B, NOT(C))))Running this prints the two-input table, shows ~1 = -2 NOT(1) = 0, and ends with F = 1. The parentheses in the last line spell out the precedence from the previous section: NOT binds tightest, then AND, then OR.
Common errors
- Reading + as arithmetic addition and concluding 1 + 1 = 2. In Boolean algebra 1 + 1 = 1.
- Applying the wrong precedence to expressions such as A + B·C. AND binds before OR.
- Treating XOR as OR. They differ only on the row A = B = 1.
- Forgetting that NAND and NOR are not associative. (A NAND B) NAND C is not equal to A NAND (B NAND C), so a three-input NAND must be defined as (ABC)'.
- Confusing (AB)' with A'B'. The first is NAND, which equals A' + B' by De Morgan's law, covered two chapters from now.
Exercises
Exercise
How many distinct Boolean functions of 3 variables exist, and how many of them evaluate to 1 on exactly one input row?
Show answer
There are 2^(2^3) = 256 functions. A function that is 1 on exactly one of the 8 rows can be chosen in 8 ways, so 8 of them have a single 1.
Exercise
Evaluate F = A·B' + C ⊕ A' for A = 1, B = 0, C = 1, treating ⊕ as having lower precedence than + (that is, F = (A·B' + C) ⊕ A'). What is F, and what would it be if ⊕ bound tighter than + (F = A·B' + (C ⊕ A'))?
Show answer
First reading: A·B' = 1·1 = 1, so A·B' + C = 1 + 1 = 1. A' = 0, so F = 1 ⊕ 0 = 1. Second reading: C ⊕ A' = 1 ⊕ 0 = 1, so F = 1 + 1 = 1. Both readings give 1 here, but they can differ for other inputs, which is why you should add parentheses when XOR is mixed with + or ·.
Exercise
Show that A ⊕ B ⊕ C is 1 exactly when an odd number of A, B, C are 1, and give the minterm numbers (with A as the most significant bit) where it is 1.
Show answer
XOR is associative, and A ⊕ B equals 1 when the inputs differ, so each extra variable flips the running result whenever that input is 1. The result is therefore 1 when the number of ones is odd. The rows with one or three ones are 001, 010, 100 and 111, which are minterms 1, 2, 4 and 7.