Skip to content
SPM Tuition
Computer Science · Boolean logic and representation

Build a truth table for a nested expression

You get simple truth tables right, but an expression with brackets and NOT throws you.

A nested Boolean expression becomes easy when you evaluate it in small pieces, one column at a time. Every expression uses only NOT, AND and OR, and each column applies just one of them.

This lesson opens Boolean logic and representation. The basics are in reading Boolean expressions and truth tables.

How do I build the table?

Follow four steps, and do not skip the second.

  1. Write the inputs and list all 2ⁿ combinations in binary counting order.
  2. Split the expression into its smallest parts, and give each a helper column.
  3. Evaluate each helper column from left to right, using only the columns before it.
  4. The last column is the expression. Copy it out as the answer.

The order of evaluation is brackets first, then NOT, then AND, then OR.

Worked example

Build the table for X = (A AND B) OR NOT C. The inputs are A, B and C, so there are 8 rows. The helper columns are A AND B and NOT C.

A B C A AND B NOT C X
0 0 0 0 1 1
0 0 1 0 0 0
0 1 0 0 1 1
0 1 1 0 0 0
1 0 0 0 1 1
1 0 1 0 0 0
1 1 0 1 1 1
1 1 1 1 0 1

X is 1 in five rows, and it is 0 only when C is 1 and A AND B is 0. Read the column out loud to test it: X is false exactly when C is on and A and B are not both on.

The mistake: ignoring precedence

The common slip is to read the expression left to right. Take A AND (B OR NOT C), which differs from the one above only in its brackets.

Evaluate the same three rows with the two expressions.

A B C (A AND B) OR NOT C A AND (B OR NOT C)
0 0 0 1 0
1 0 1 0 0
1 0 0 1 1

In row 1, A is 0, so the second expression is 0 whatever is inside the bracket. The first expression is 1 because NOT C is 1. The two expressions give different answers in a row where C is 0 and A is 0, so brackets change the function, not only its appearance.

Checking your table

Three quick checks catch most errors.

  • Count the rows: is there one row for every input combination?
  • Test one row by hand, from the original expression and not from your columns.
  • Check that no helper column uses a value from a column to its right.

Use the Boolean expression and truth-table explorer to compare your final column after you have finished.

Check yourself

Build the truth table for Y = NOT (A OR B) AND C. List all rows and give the number of rows where Y is 1.

Answer

There are 3 inputs, so 8 rows. Helper columns: A OR B, NOT (A OR B), then Y.

A B C A OR B NOT (A OR B) Y
0 0 0 0 1 0
0 0 1 0 1 1
0 1 0 1 0 0
0 1 1 1 0 0
1 0 0 1 0 0
1 0 1 1 0 0
1 1 0 1 0 0
1 1 1 1 0 0

Y is 1 in exactly 1 row: A = 0, B = 0, C = 1. The NOT applies to the whole bracket (A OR B), so it is evaluated after the OR inside the bracket.

What to study next

The next lesson uses the same tables to test a claim: checking a claimed equivalence with all input combinations. Then test the cluster in the integrated practice set.

If you want a teacher to check your helper columns with you, see online one-to-one Computer Science tuition.

Common questions

How many rows does a truth table need?

Two to the power of the number of inputs. Two inputs need 4 rows, three inputs need 8 and four inputs need 16. List the input combinations in binary counting order so that none is missed or repeated.

In what order are NOT, AND and OR evaluated?

Brackets first, then NOT, then AND, then OR. If a question gives brackets, follow them. If it gives none, use that order, and add brackets to your own working to make the order obvious.

Why use helper columns?

Evaluating a whole nested expression in one step invites mistakes. A column for each sub-expression lets you check each small result, and a wrong final column can be traced to the exact helper column.

Does the order of input columns matter?

The final output does not depend on it, but using a consistent order such as A, B, C from left to right, counting up in binary, stops rows from going missing and lets you compare your table with a model answer.

If your final column keeps disagreeing with the model answer, one-to-one Computer Science lessons let a teacher check your helper columns and find the step where the evaluation goes wrong.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.