Boolean Logic and the C Language
Part 9 of Complete C Course
Foreword: Understand Boolean Logic
By: Chrysanthus Date Published: 31 May 2024
Introduction
The Issue
Assuming that I am tall and you are tall. If someone ask you, if both of us are tall, you would say, Yes (true). If he asks if both of us are short, you would say, No (false). If you are short and I am tall, and he asks you if either you or me is tall, your answer would be Yes (true). If he asks if both you and me are tall, you would not have an answer. You might go on to say that the last question should not be asked, or that the question does not have an answer. Well, I want you to know as from today, that under certain circumstances the question should be asked.
In biology, a person is either tall or short. It is "environmental" conditions that make the person to have a medium height. One scientist, George Boole defined a set of answers or rules for this kind of questions. These rules are studied in this section of the chapter. These rules are used in computing, programming, electronics and telecommunications, today. In fact, without these rules, you would not have a computer; you would not also have programming.
True or False
A simple human language statement is either true or false in itself. If I say, "I am tall", that is either true or false. If I say, "you are tall", that is either true or false. If I am tall and you are short, and the question is asked, if both you and me are tall, in Boolean Logic, an answer of true or false must be given. Which of these two should be given? Boole did not really answer this question. He simply came up with a set of rules for us to follow. The good news is that, when you follow these rules in their right context, you do not have any ambiguity. Thanks to these rules, we have computers and programming, today. These rules are given to you next. The rules cannot really be explained; you just accept them. The rules are under three headings: AND, OR and NOT.
AND
The question can be asked, if both you AND me are tall. My height and your height are combined by AND. These are the rules to follow:
false AND false = false
false AND true = false
true AND false = false
true AND true = true
Now, let tall be true and short be false. This means that if I am short AND you are short, you and me are short. If I am short AND you are tall, you and me are short; that is Boolean answer, which you have to accept. If I am tall AND you are short, both you and me are short. If I am tall AND you are tall, you and me are tall. All these are Boolean rules that you just have to accept.
With C, true is represented by 1 and false is represented by 0. The layout of the truth table, follows binary counting.
OR
The question can be asked, if you OR me is tall. My height and your height are combined by OR. These are the rules to follow:
false OR false = false
false OR true = true
true OR false = true
true OR true = true
Again, let tall be true and short be false. This means that if I am short OR you are short, you or me is short. If I am short OR you are tall, you or me is tall. If I am tall OR you are short, you OR me is tall. If I am tall OR you are tall, you or me is tall. All these are Boolean rules that you just have to accept.
NOT
Now, in Boolean logic, only two states (possible answers) exist. That is, if you are NOT tall, you are short; if you are NOT short, you are tall; nothing else. These are the rules to follow:
NOT false = true
NOT true = false
Assume that you have a string (spring), you can extend (pull). While the string is in its natural state, if I say, "NOT short" you would extend it; that is the interpretation. While the string is extended, if I say, "NOT long" you would allow it to contract; that is the interpretation.
You have to memorize all the above rules in their different categories.
More than two Variables
In C, AND, OR and NOT are each called an operator. For the NOT operator, only one operand (true or false) is needed to give an answer. For AND or OR, there are two operands. The above cases show two operands for AND or OR and one operand for NOT. There can even be three operands and more; but only one answer. NOT always has only one operand and one answer. The situation for three operands, for the AND operator, is as follows:
false AND false AND false = false
false AND false AND true = false
false AND true AND false = false
false AND true AND true = false
true AND false AND false = false
true AND false AND true = false
true AND true AND false = false
true AND true AND true = true
There are eight lines when the operands are three. When the operands are four, there are 16 lines.