bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Programming/Programming Concepts
Programming•Programming Concepts

Boolean Algebra

Boolean algebra is math that deals with operations on Boolean values.

"Boolean" is written with an upper case first letter because it's named after a person: George Boole (1815-1864), who developed this algebra of logic.

What is Boolean Algebra?

Boolean algebra is the study of what happens when logic operations (AND, OR, NOT) are used on Boolean values (either true or false ).

Boolean algebra helps us understand how computers and digital electronics work, and how to simplify logic expressions.

Check out our page about logical operators to see how logic operations AND, OR, and NOT are used in programming.

Different Representations of Boolean Algebra

Boolean algebra can be expressed in different ways, depending on the context.

Below are how the logic operations AND, OR, and NOT can be represented in math, and in programming:

Logic OperationMathProgramming
A AND BA · BA && B
A OR BA + BAB
NOT Anot(A)!A

Most of this page is dedicated to Boolean algebra as math, but there are some programming examples in between, and an explanation of logic gates further down.

See our page about logical operators to see more about how these operators are programmed.

AND, OR, and NOT

Before we start looking at Boolean algebra, we need to be sure how the AND, OR, and NOT operations work.

Note

In Boolean algebra, we use 1 instead of true and 0 instead of false .

AND takes two Boolean values. The result is only true if both values are true , otherwise it is false .

ABA AND B
111
100
010
000

OR takes two Boolean values, and is true if at least one of the values is true , otherwise it is false .

ABA OR B
111
101
011
000

NOT takes one Boolean value, and makes it the opposite. If the value is false , the NOT operation on that value will return true , and if the value is true , the NOT operation on that value will return false .

ANOT A
10
01

Doing the NOT operation "NOT A", we often say "the complement of A", "A bar" (written as not(A)), "A negated", "A prime" (written as A'), or simply "NOT A".

Writing Boolean Algebra

These are the components used to write Boolean algebra:

  • true is written as 1
  • false is written as 0
  • AND is written using multiplication symbol (·)
  • OR is written using addition symbol (+)
  • NOT is written using overline (not(A))

AND, OR, and NOT can also be written using symbols ∧, ∨, and ¬, but we will use the symbols stated in the list above.

The Order of Operations

Like there are rules for what operations we do first in normal math, there is also an order of operations for Boolean algebra.

Before going on to more complex Boolean algebra, we need to know the order of operations.

  • Parentheses
  • NOT
  • AND
  • OR

For example, in this expression:

1 + 0 · 0

The correct order is to do AND first, so 0 · 0, the initial expression is reduced to:

1 + 0

Which is 1 ( true ).

So solving the expression in the correct order:

1 + 0 · 0 = 1 + 0 \8pt] = 1

Solving this expression with the wrong order, doing OR before AND, would result in 0 ( false ) as the answer, so keeping to the correct order of operations is important.

Boolean Algebra with Variables

After establishing the basic concepts of Boolean algebra, we can finally start seeing more useful and interesting results.

Boolean variables are usually written in uppercase, like A, B, C, etc.

We need to think about a Boolean variable as unknown, but it is either true or false .

Below are some basic Boolean algebra results we get, using variables:

A + 0 = A \8pt] A + 1 = 1 \8pt] A + A = A \8pt] A + not(A) = 1 \8pt] A · 0 = 0 \8pt] A · 1 = A \8pt] A · A = A \8pt] A · not(A) = 0 \8pt] not(not(A)) = A \8pt]

The results above are simple, but important. You should go through them one by one and make sure you understand them. (You can replace variable A with 1, see if it's correct, and then replace A with 0, and see if it's still correct.)

Simplifying Code Using Boolean Algebra

The rules above can be used to simplify code.

Let's look at a code example, where a condition is checked to see if a person can borrow a book from the university library.

Runnable example

if is_student and (age < 18 or age >= 18):
  print("You can borrow a book from the university library")

The condition in the if statement above

is\student AND (age < 18 OR age ≥ 18)

can be written using Boolean algebra, like this:

is\student · (under18 + not(under18))
A · (B + not(B))

From the list of Boolean algebra results above, we see that

B + not(B) = 1

(We know this rule from the list of Boolean algebra results in the previous section.)

So the condition in the if statement can be simplified:

is\student · (under18 + not(under18)) \8pt] = is\student · (1) \8pt] = is\student

The result is that we don't have to check the age at all to see if the person can borrow a book from the university library, we just need to check if they are a student.

Runnable example

if is_student:
  print("You can borrow a book from the university library")

The condition is simplified

So checking the student ID is enough, no need to check their age to see if they are allowed to borrow a book.

You might be able to see how the condition can be simplified without the use of Boolean algebra, but in more complex expressions, Boolean algebra can be very useful.

Boolean Algebra Laws

In addition to the basic Boolean algebra laws listed in the previous section, we also have more complex laws.

The commutative law just shows us that the order of the variables does not matter.

A · B = B · A
A + B = B + A

The distributive law tells us that we can distribute the AND operation over the OR operation.

A · (B + C) = A · B + A · C
A + B · C = (A + B) · (A + C)

The first law above is quite straightforward and similar to the distributive law in normal algebra.

But the second law aboveis not that obvious, so let's see how we can arrive at the same result, starting with the right hand side:

(A + B) · (A + C) \8pt] = A · A + A · C + B · A + B · C \8pt] = A + A · C + A · B + B · C \8pt] = A · (1 + C + B) + B · C \8pt] = A · 1 + B · C \8pt] = A + B · C

The associative law tells us that we can group the variables in different ways, without changing the result.

(A · B) · C = A · (B · C)
(A + B) + C = A + (B + C)

De Morgan's Laws

De Morgan's laws are two widely used and recognized laws in Boolean algebra.

De Morgan's first law.

The complement of a product is the same as taking the sum of the complements.

not(A · B) = not(A) + not(B)

The word complement is used in Boolean algebra meaning the opposite, to negate something, or using the NOT operator. The complement of A is written as not(A).

Below is an an example of how a condition can be re-written and work exactly the same way, using De Morgan's first law.

Let's say a tank in a production process is safe if both the temperature and pressure in it are below certain limits.

tmp < 100 AND press < 20 = Safe

In the oposite case, the tank is not safe, and we should sound the alarm.

not(tmp < 100 AND press < 20) = Alarm

Using De Morgan's first law, we can rewrite the expression:

not(tmp < 100 AND press < 20) \8pt] = not(tmp < 100) OR not(press < 20) \8pt] = tmp ≥ 100 OR press ≥ 20

The result we have arrived at here is both easier to understand, and to program, and since we've used De Morgan's first law correctly, we can be sure that the condition will work the same way as the original.

De Morgan's second law.

The complement of a sum is the same as taking the product of the complements.

not(A + B) = not(A) · not(B)

For example, if you say "I do not have dogs or cats"

not(haveDogs + haveCats)

You might as well say "I do not have dogs and I do not have cats"

not(haveDogs) · not(haveCats)

Those two statements are the same, and they follow De Morgan's second law.

Simplifying a Complex Expression Using Boolean Algebra

Imagine a security system with sensors to detect open windows and doors, and sensors for motion detection.

  • open window W
  • open door D
  • motion detected in kitcken M_K
  • motion detected in living room M_L

These are all the different conditions, or scenarios, that should trigger the alarm:

  • Motion detected in living room AND window is open (M_L · W)
  • Motion detected in living room AND door is open (M_L · D)
  • Motion detected in kitchen AND window is open (M_K · W)
  • Motion detected in kitchen AND door is open (M_K · D)

Using Boolean algebra, when this expression is true , the alarm will sound:

(ML · W) + (ML · D) + (MK · W) + (MK · D)

Perhaps you see how this could be simplified right away? But even if you do see it, how can you be sure that the simiplified expression works the same way as the original?

Let's use Boolean algebra to simplify the expression:

(ML · W) + (ML · D) + (MK · W) + (MK · D) \8pt] = ML · W + ML · D + MK · W + MK · D \8pt] = ML · (W + D) + MK · (W + D) \8pt] = (ML + MK) · (W + D) \8pt]

Using Boolean algebra, we have simplified the expression.

The alarm will sound if motion is detected in the living room or kitchen, if at the same time the window or door is open.

Logic Gates

A logic gate is an electronic device made of transistors that implements a logical operation (Boolean function) AND, OR, or NOT. Other common logic gates are NAND, NOR, XOR, and XNOR.

Click on inputs A and B below to toggle them between 0 and 1, and click on the gate to cycle through the different logic gates.

Logic gates are used everywhere in computers and electronic devices: headsets, cars, mobile phones, SSD, RAM, CPU, etc.

Below is an overview of the most common logic gates.

Logic OperationLogic GateMath
ANDA BA · B
ORA BA + B
NOTAnot(A)
NANDA Bnot(A · B)
NORA Bnot(A + B)
XORA BA oplus B = A·not(B) + not(A)· B
XNORA Bnot(A oplus B) = A· B + not(A)·not(B)

The logic gates for AND, OR, and NOT are the basic logic gates, and works just like described in the previous sections.

The logic gates for NAND and NOR are just the opposite of AND and OR, so they will not be described any further here.

But the logic gates for XOR and XNOR are a bit more special.

The XOR gate checks if the inputs are different A ≠ B, and outputs 1 if they are. XOR is like the running A != B in programming.

The XNOR gate checks if the inputs are equal A = B, and outputs 1 if they are. XNOR is like the running A == B in programming.

The truth tables for XOR and XNOR look like this:

ABA XOR B
110
101
011
000
ABA XNOR B
111
100
010
001

Previous

Hexadecimal Numbers in Programming

This chapter

Overview
25

Lessons

105m

Read time

1. Understanding The Concepts of Programming2. What is Programming ?3. Variables in Programming4. Constants in Programming5. If Statements in Programming6. Arrays in Programming7. Loops in Programming8. Functions in Programming9. Recursion in Programming10. Scope in Programming11. Strings in Programming12. Data Types in Programming13. Type Casting in Programming14. Operators in Programming15. Arithmetic Operators in Programming16. Assignment Operators in Programming17. Comparison Operators in Programming18. Logical Operators in Programming19. Bitwise Operators in Programming20. Comments in Programming21. Input and Output in Programming22. Bits and Bytes in Programming23. Binary Numbers in Programming24. Hexadecimal Numbers in Programming25. Boolean Algebra

On this page

What is Boolean Algebra?Different Representations of Boolean AlgebraAND, OR, and NOTWriting Boolean AlgebraThe Order of OperationsBoolean Algebra with VariablesSimplifying Code Using Boolean AlgebraBoolean Algebra LawsDe Morgan's LawsSimplifying a Complex Expression Using Boolean AlgebraLogic Gates