Boolean Logic

There are interactive resources on Boolean logic and logic circuits for KS3 students in the Interactive section. Truth tables appear in GCSE Computer Science.

Boolean logic (or Boolean algebra, as it is sometimes called) is based on the work of the nineteenth century British mathematician George Boole. It is similar to the conventional algebra that you may have encountered in Maths lessons, but each of the variables can represent only one of two states, rather than a number or value - these are known as truth values, e.g. true or false.

Truth Values

Some statements can be seen to be either true or false. For example, this is a page about Boolean logic (true), or this page has a red background (false). These sorts of statement or ideas are the truth values that form the basis of Boolean logic.

Other statements are a matter of opinion, e.g. it's warm today, or that's a nice hat you're wearing - Boolean logic cannot help with these.

Variables

The variables (i.e. the letters) used in Boolean logic are used to represent these truth values and can therefore only take one of two states, or values. These are regarded as opposites, and as well as being described as true and false, can also be described as 0 and 1, or on and off. For example, you will find that some power swithches are labelled 0 and 1 rather than on and off.

These different names mean the same thing, and are often used interchangeably, such that true = 1 = on and false = 0 = off.

Truth Tables

The results of calculations using Boolean logic are often displayed in truth tables. These list every possible value for the input variables, and then show what the output (result) would be for those inputs. The tables you see in the following sections are examples of truth tables.

A simple way to complete the inputs section of a truth table is to alternate the bits in the first column (i.e. 010101...), and then in each subsequent column alternate the bits with half the frequency (e.g. 00110011..., then 000011110000..., etc.).  You can see this pattern of bits when you count in binary.

NOT

The NOT operator is the simplest to understand. It is a unary operator (i.e. it only takes one value); it simply takes a value and returns the opposite of that value. So NOT true is false and NOT false is true. The truth table for this function is shown below:

A NOT A
0 1
1 0

NOT A can also be written as either ¬A or A (i.e. an A with a line over it).

AND

The output of the AND function is true only if all of the inputs are true. That is to say that if you have two variables, A and B, then A AND B is only true if both A is true AND B is true. Obvious, really!

A B A AND B
0 0 0
0 1 0
1 0 0
1 1 1

The AND function can also be represented by a dot (.) or the ∧ symbol , so A AND B can also be written A.B or A∧B - the former can be helpful because the AND operator behaves in a similar way to the multiplication operator in arithmetic (see below).

OR

The output of the OR function is true if any of the inputs are true. That's to say that if you have two variables, A and B, then A OR B is true if either A is true OR B is true. Obvious, really!

A B A OR B
0 0 0
0 1 1
1 0 1
1 1 1

The OR function can also be represented by a + sign or ∨ symbol , so A OR B can also be written A + B or A V B - the former is useful because the OR operator behaves in a similar way to the addition operator in arithmetic (see below).

Exclusive-OR

Unlike the AND and OR operators, Exclusive-OR (often written as EOR or XOR) can only be used with two variables. The result of A EOR B is true if either A is true or B is true, but not both. Or, in simpler terms, the result of A EOR B is true only if A and B have different values.

A B A EOR B
0 0 0
0 1 1
1 0 1
1 1 0

The exclusive-OR operator is also represented by a ⊕ symbol (i.e. a + with a circle around it) and ⊻.

NAND and NOR

A B A NAND B
0 0 1
0 1 1
1 0 1
1 1 0

There are no special symbols to represent the NAND and NOR operators - just combine the NOT, and AND or OR symbols. A NOR B, therefore, could be written ¬(A + B).

A B A NOR B
0 0 1
0 1 0
1 0 0
1 1 0

In electronic logic circuits, NAND and NOR gates are often used to reproduce a NOT gate. This can be done simply by connecting together both inputs - i.e. A NOR A = NOT A, and also A NAND A = NOT A.

Operator Precedence, etc.

Just as different arithmetic operators have different properties, so do logical operators. They have an order of precedence (like BODMAS/BIDMAS for arithmetic), and associative, commutative and transitive properties. They are quite simple to understand, though.

The OR operator behaves in the same way as addition, and the AND operator behaves like muliplication. This is why you sometimes see p OR q written as p + q, and p AND q written as p.q. This even applies when parentheses are used, so a AND (b OR c) is the same as (a AND b) OR (a AND c) - because a.(b + c) is the same as a.b + a.c!

De Morgan's Laws

These probably aren't a great deal of use in programming, but there are circumstances when you might want to turn your ANDs into ORs, or vice-versa. One situation where you might want to do this is in the construction of electronic logic circuits, where an AND can be represented using transistors. De Morgans laws allow you to do this.

The two forms are as shown below:

¬(A + B + C + ...) = ¬A.¬B.¬C...

¬(A.B.C...) = ¬A + ¬B + ¬C...

These laws can be expanded and work with any number of variables.

Duality

You may also have noticed that there is a relationship between the AND and OR functions. Look at the truth tables - they exhibit a property called duality. This means that if you take one of the truth tables (e.g. AND) and change all the 0s to 1s, and 1s to 0s, then you get the other table (e.g. OR). Interesting, but not especially useful!

Use in Programming

Obviously, Boolean logic can be very useful for making decisions in programs, particularly in conjunction with the if... then... type statements that most languages have. Furthermore, you don't normally need to supply the value when testing Boolean variables, so that if you have a Boolean variable called post, then if post then... is equivalent to if post = true then...

Most programming languages have a Boolean type which can take the values true and false, and lots of programming languages include true and false as constants so that you can refer to them by name.

In most cases, however, Boolean variables behave like integers (i.e. whole numbers), with false represented by 0 (zero) and true represented by a non-zero value, usually 1 or -1. This means that you can use Boolean variables in calculations.

Imagine you were creating an ordering system for a shop that also does mail order. If the goods are to be posted, then you'll need to add the cost of postage. If you had a Boolean variable called post, which tells you whether the item is to be posted, then you could calculate the total cost as cost_of_goods + abs(postage_cost * post).

You can also use Boolean operators in Excel, using the =AND() and =OR() functions - see the Excel page for more details.