Wednesday, September 23, 2015

Boolean Logic

Boolean logic is a way of describing relationships between ideas that can have the truth values TRUE and FALSE. We describe these relationships in a diagram called a truth table:

xyx OR yx AND y
FalseFalseFalseFalse
FalseTrueTrueFalse
TrueFalseTrueFalse
TrueTrueTrueTrue

Here is an example of boolean logic. Suppose we have a set of colored shapes. We might ask two kinds of questions. If we choose one of the colored shapes, we might ask: is it blue and a circle? If we choose a blue circle, it is blue and also a circle. If we choose a yellow circle, it is not blue and a circle, because it is yellow (not blue). If we choose a blue square, it is not blue and a circle, because it is a square (not a circle). So whatever shape we choose must satisfy both the requirements, that it be both blue and also a circle.


Now, if we choose one of the colored shapes, we might ask: is it either blue or a circle? If we choose a yellow square, it is obviously not blue, and it is not a circle, so it is not either blue or a circle. But if we choose a blue square, it is either blue or a circle, because it is blue, even though it is not a circle, because we had to satisfy only one of the requirements, that is, that it be either blue or a circle. It need not satisfy both the requirements.

In the same way, if we choose a yellow circle, it satisfies the requirement, because it is a circle, even though it is not blue, because we had to satisfy only one of the requirements.

We can draw a truth table for this specific situation like this:

ColorShapeBlue OR circle?Blue AND Circle?
BlueCircleTrueTrue
YellowCircleTrueFalse
BlueSquareTrueFalse
YellowSquareFalseFalse

We can think about these in relation to more realistic situations. In computer programming, we might need to determine if a number falls within a certain range. This can become confusing because we are mixing numbers, which can have many values, with booleans, which can have only the values True and False.

In the following table, the variable x can take on any numerical value, and 5 is obviously a numerical value, but the expression x>5 can have only two different values: True or False. Either x is greater than 5, or x is not greater than 5. There are no other choices. So we have a numerical expression that uses a comparison operator (> or greater than), and the expression evaluates to a boolean value that is either true or false. This operation that "converts" a wide range of numerical values to the narrow range of boolean values (True and False) is hard to grasp, but for computers, it makes sense and it works.

xx > 5x < 10x > 5 OR x < 10x > 5 AND x < 10
7TrueTrueTrueTrue
4FalseTrueTrueFalse
11TrueFalseTrueFalse



No comments:

Post a Comment