Introduction to Boolean Algebra 1

Boolean algebra is a way of describing logic in a mathematical way. In boolean algebra you work with two states – true and false – rather than numbers. 1 is used to denote true and 0 is used to denote false. The three most basic operations in boolean algebra are and, or, and not. These operations can be chained together to determine the truth of a statement.

The And Operator

The and operator which sometimes uses the symbol is the first operator that we will be using. The following is a truth table that can be used to determine the result of anding two values together.

OperationResult
true truetrue
true falsefalse
false truefalse
false falsefalse

This should give you a better idea of how logical operators work. The and operator will take two boolean values and will give back a single boolean value. If you look at the truth table for and you can see that and will only be true when it is given two true values. Here is a longer example using ands:

(true ∧ false) ∧ (true ∧ true)
(false) ∧ (true)
false

Super simple so far! Let’s look at the next operator.

The Or Operator

Similar to the and operator, the or operator takes two boolean values and returns a single boolean value. The symbol for the or operator is . Let’s take a look at the truth table:

OperationResult
true truetrue
true falsetrue
false truetrue
false falsefalse

So the or operator is sort of the opposite of the and operator. The and operator is only true when both values are true but the or operator is only false when both values are false. Let’s take a look at the same example from earlier using ors instead.

(true ∨ false) ∨ (true ∨ true)
(true) ∨ (true)
true

Very similar to and. The next operator will work a little differently but is probably the most intuitive.

If you are getting confused between the and operators, here is a little trick I use to help myself remember:

Negation with the Not Operator

The not operator (which uses the ! or ¬ symbol) is a little bit different because it affects a single value rather than operating on multiple values. Here is the truth table:

OperationResult
¬(true)false
¬(false)true

All the not operator does is flip the value of a statement! Now let’s try some equations using all three of the operators.

A = true
B = false
(¬A V B) Λ (A V B)
(false V false) Λ (true V false)
false Λ true
false

Now that we have seen how these basic operators work, let’s look at how they can be applied when writing code.

Applications in Computer Programming

Let’s imagine that we have a program that is performing some kind of user access functionality. Take a look at the psuedocode below.

bool authenticated = authenticateUser(username, password);
string userType = getUserType(username);

if(authenticated && userType = "admin")
{
    printf("Welcome admin! \n");
}
else if (authenticated && userType != "admin")
{
    printf("Welcome %s \n", getUserName(username));
}
else
{
    printf("Login failed. Please try again");
}

Control flow for the program is being managed by the same type of logical equations that we just solved. You’ll notice that I am using the && (logical and operator) – the C programming language equivalent of Λ. If the equation evaluates to true then we enter the code block. If it does not, then we skip to the next block and do any evaluations there. Here we can see two logical equations:

authenticated && userType = "admin"
authenticated && userType != "admin"

Let’s convert these to the symbolic logic notation from earlier in this post.

authenticated = true
(userType = "admin") = false
true && false
true Λ false
false

Since the first equation evaluated to false, we now have to evaluate the second equation.

authenticated = true
(userType = "admin") != true
true && true
true Λ true
true

Since this equation evaluated to true will execute the second block of code printing “Welcome” and the users username.

In the next tutorial I will cover more logical operators.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: