Arduino

Arduino Boolean Operators

When writing an Arduino code for any specific project different operators will be needed for performing various functions, they can either be mathematical or logical operations and for each type of functions there will be specific operators. Here in this article the Boolean operators are discussed briefly.

Boolean Operators

To perform the logical operations Boolean operators are used and these operators operate in binary numbers that are 0 and 1. The input and the output can also be in the form of true and false, that is if there is one at the output it will be true and in case of zero it will be false. There are three basic types of Boolean operators that are most commonly used in Arduino programming:

  • AND boolean Operator
  • NOT boolean Operator
  • OR boolean Operator

AND Operator

The first logical operator is the AND operator whose output will be true only if both the conditions given to the operator are true. Similarly, if any one of the inputs of the operators is false or zero the output will be false.

To use this operator the sign “&&” is used. For instance, if the value for A is one and value for the B is zero and the operator AND is applied (A&&B) the output will be zero or false. Note that the output of AND operation will only be true if both the inputs are true. For further understanding an example code of the AND operation is given using if-else statements.

void setup(){

  Serial.begin(9600);

   int a = 15;

   int b = 14;

   bool d;

   if((a >b) && (b <a)) {

     d=true;

     Serial.print("AND operation: ");

     Serial.println(d);
 
     }

   else

   {

      d= false;

      Serial.print("AND operation: ");

      Serial.print(d);

     }

}


void loop () {

}

Output

NOT Operator

The second Boolean operator is the NOT operator which is used where the output needs to be inverted. This operator is represented by the mark of exclamation (!). This operator has only one input and one output. If there is zero at the input of the operator it will convert it into one.

For example the variable a has value of 1 and a NOT operator (!A) is applied to it then the value of the variable will be 0 at the output. Further the operation of the NOT operator can be understood by the Arduino example code. In the example by using the not operator the output of AND operation is inverted from one to zero.

void setup(){

  Serial.begin(9600);

   int a = 15;

   int b = 14;

   bool d;

   if(!(a >b) && (b <a)) {

     d=true;

     Serial.print("NOT operation:");

     Serial.println(d);

     }

   else

   {

      d= false;

      Serial.print("NOT operation :");

      Serial.print(d);

     }

}


void loop () {

}

Output

OR Operator

The third and the last operator used for performing the logical function in the Arduino programing is the OR function. Unlike the AND operator this Boolean function gives the output one if any of the inputs are one or true. So it can be said that the output of the OR function will be false or zero when both the inputs are zero. The sign used for this operator is “||”.

To further explain the function of the operator we can assume that if A and B are the inputs of the OR operator and A has value of zero and B has a value of one then the OR operator (A||B) will give one as an output. The OR operation is further explained with the help of an example code.

void setup(){

  Serial.begin(9600);

   int a = 15;

   int b = 14;

   bool d;

   if((a >b) || (b <a)) {

     d=true;

     Serial.print("OR operation:");

     Serial.println(d);

     }

     
   else

   {

      d= false;

      Serial.print("OR operation :");

      Serial.print(d);

   }

}


void loop () {

}

Output

A summary of all boolean operators is mentioned in the table below:

Inputs Outputs
AND OR NOT
A B A && B A || B !A
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0

Conclusion

The boolean or logical operators are used for the determining of the conditions on which the program for a specific task will run. This write-up briefly explains what Boolean operators are, what their types are and how they can be used in the Arduino program. Examples of Arduino programs are also given which gives a clear concept for the functionality of Boolean operators.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.