## Boolean Algebra-Chocolate OR Vanilla?

12 Sep 2012

Welcome to a land where 1 + 1 = 1.

After writing a horrible ternary operator in Ruby, it was time for a whiteboard session about boolean algebra, AND, and OR.

Unlike conventional algebra, Boolean algebra is meant to be more abstract. George Boole divorced his algebra from numbers directly and instead refers to classes or groups of things(a.k.a. sets). Values used in this algebra are 0(or false) and 1(or true). Operators may look the same as symbols in math that you are used to but they stand for different ideas. The + operator stands for union. Here is an example. Let's say you are talking about cupcakes. Lets talk about 2 types of cupcakes,(aka two classes of cupcakes). Let's assume that we do in fact have at least 1 each of the types of cupcakes (C)hocolate and (V)anilla. C + V , Chocolate UNION Vanilla, means that you want everything in the Chocolate class combined with everything in the Vanilla class. So C + V(aka C union V) = All cupcakes that are Chocolate and all cupcakes that are Vanilla. Another way you could say that is All cupcakes that are either chocolate OR vanilla. In programming, that expression would actually be represented as an OR operator and look like this in Ruby...

```
1 if Chocolate || Vanilla
2 puts "I am so glad there are cupcakes!"
3 else
4 puts "You'd better come up with some cupcakes ASAP!!!!!!!!"
5 end
6
```

Now, if we again think about the fact that the values in this algebra represent true(1) or false(0). We can look at the expression again and think about it numerically. Remember when I said that we could assume that we do have at least 1 of each type of cupcake? That means that Chocolate is true and so is Vanilla. Well then our expression really could be broken down into this...

```
1 if Chocolate (is true(1)) || Vanilla (is true(1))
2 puts "I am so glad there are cupcakes!"
3 else
4 puts "You'd better come up with some cupcakes ASAP!!!!!!!!"
5 end
6
```

The result is "I am so glad there are cupcakes!"

Now lets pretend we have no Chocolate cupcakes and no Vanilla cupcakes.

```
1 if Chocolate (is true(1)) || Vanilla (is true(1))
2 puts "I am so glad there are cupcakes!"
3 else
4 puts "You'd better come up with some cupcakes ASAP!!!!!!!!"
5 end
6
```

They are both false, so he result is "You'd better come up with some cupcakes ASAP!!!!!!!!"

Now lets pretend we have no Chocolate cupcakes but we do have Vanilla cupcakes.

```
1 if Chocolate (is true(1)) || Vanilla (is true(1))
2 puts "I am so glad there are cupcakes!"
3 else
4 puts "You'd better come up with some cupcakes ASAP!!!!!!!!"
5 end
6
```

Even though we have no Chocolate cupcakes, we do have Vanilla cupcakes, so the result is "I am so glad there are cupcakes!"

This operator holds true to the commutative property, which means you can switch around the symbols on either side of the operations. C+V = V+C. They are the same.

As if that was not cool enough already, this whole idea can be put into a nifty little thing called a truth table. It looks like this...

Pretty cool, Huh!

Enjoy!!!