True or False? An Introduction to Boolean Algebra

True or False? An Introduction to Boolean Algebra

8 June 2021

An android walks up to a street food stand and asks: "One small fries, please". The vendor asks back: "Ketchup or mayo?", to which the android responds "yes".

This joke's a bit of a long shot so please bear with me. To get to the bottom of it, we're going to talk about Boolean algebra, a branch of mathematics first introduced by English mathematician George Boole in 1847. Although the word "Boolean" was coined as an adjective to describe this branch of mathematics, modern programmers often use it as a noun to represent a variable with the Boolean data type.

A data type is a concept programming languages use to translate abstract concepts into binary data. Using the binary values 1 and 0 to represent true and false, Boolean is the simplest data type, requiring only one bit to represent a variable.1^1 A variable is a feature of programming languages which allows the programmer to store data on the computer under a given name. In web development, for example, we might have a Boolean variable named isUserLoggedIn whose value (true or false) we could read to determine whether or not to show sensitive information to the client.

As we can see, the Boolean type is a fundamental building block of modern software. Using Boolean algebra, we can combine and manipulate Boolean variables to further increase their potential. The basic operations of Boolean algebra are AND, OR, and NOT. One simple way to get to know them is using truth tables:

AND/OR/NOT Truth Tables

Back to our silly joke. The fries vendor wants to know whether the android wants ketchup or mayonnaise (or both). Since the android is not very adept at interpreting the subtleties of human interaction, it simply evaluates the question by consulting the OR table. Since the android wants ketchup (but no mayo), it determines the logical answer to the question to be "yes" (as opposed to "no"). This is where the android fails the Turing test.2^2

Let's see how these basic operations can help us in building modern software. Take another example from web development: In an online shop, the user clicks to go to checkout. We want to make sure that both (1) the user is logged in before seeing the checkout page and (2) the user's cart is not empty. In Python, we can use the AND operator and write:

if isUserLoggedIn and isCartNotEmpty:
    # Navigate to checkout page
    # Stay on the current page

If this is the first time you read Python code, there's a bit to unpack here. Python is not the most common programming language on the web but we have chosen it here since it's rather easy to read without prior knowledge.

Lines starting with # are known as "comments". This means that the computer (using a compiler or interpreter)3^3 will ignore them when running the program, allowing the programmer to provide information to other humans reading the code. The if: ... else: ... pattern is called an "if statement". If isUserLoggedIn and isCartNotEmpty evaluates to true, the interpreter will evaluate the code in the if block: # Navigate to checkout page and skip the else block. If it evaluates to false, the interpreter will instead skip the if block and evaluate only the else block.

When writing computer code, pay extra attention to making your code easy to read and predict. This will help you maintain it in the future as well as make it easier for others to contribute. We can improve the code snippet above in this regard by changing the second Boolean using the NOT operator, like so:

if isUserLoggedIn and (not isCartEmpty):
    # Navigate to checkout page
    # Stay on the current page

Notice that we have changed the name of the Boolean variable from isCartNotEmpty to isCartEmpty. This assumes that the code which sets the value of isCartEmpty is written accordingly.

Let's rewrite this example once more to illustrate the OR operator. Imagine that we have certain conditions under which we allow the user to proceed to checkout without being logged in. We can evaluate these conditions and store the result in the Boolean variable isGuestCheckoutAllowed. We can then use the OR operator to write:

if (isUserLoggedIn or isGuestCheckoutAllowed) and (not isCartEmpty):
    # Navigate to checkout page
    # Stay on the current page

This is a basic example but we can already see the potential for Boolean data types in real code. There are more methods for combining and manipulating Booleans but these basic operators will satisfy the 80/20 rule.4^4

1^1 The integer data type, for example, requires 8 bits to represent whole numbers in the 0–255 range.

2^2 The Turing test was introduced by Alan Turing in 1950. In order to pass the test, a machine must converse with a human in such a way that a human evaluator cannot reliably tell the machine from the human.

3^3 Compilers and interpreters are computer programs with the common goal of translating human-readable code (such as the Python snippets in this article) into a running program.

4^4 The 80/20 rule has many forms. Our version states that 80% of our tasks can be completed with 20% of the total effort.


Computer Science