Understanding Boolean Logic

23 May

Ok, so on first look, Booleans are black and white. Things are either true or they’re false. But what happens if something is either true or false or true and false? If you fire up irb in your Terminal (simply type irb), you get the following:

1.9.3-p0 :024 > false and true
 => false
1.9.3-p0 :025 > true and false
 => false
1.9.3-p0 :026 > false or true
 => true
1.9.3-p0 :027 > true or false
 => true

Have you ever thought about why these are the answers?


This morning, I watched the first few minutes after 13:27 of the below MIT Lecture, and everything started to make sense!

It’s great to think of Booleans in concrete terms as switches. Here’s a photo of an “AND” faucet that I found (via turbosquid):

As you can see, both valves have to be open for the water to flow through.

If only the second valve is open, water will not go through:

false and true = false

If only the first valve is open, water will not go through:

true and false = false

Only when both valves are open will water finally be able to get through:

true and true = true

And here is a photo of an “OR” faucet (via remodelista):

This is more of the type of faucet we find in our home.

If both valves are open, water will flow through:

true or true = true

If only one valve is open, water will still flow through, so:

true or false = true

false or true = true.

Only if both valves are closed, will water not flow:

false or false = false.

Now see if you can solve these boolean exercises 🙂

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s