# Arduino Operators

The operators are widely used in Arduino programming from basics to advanced levels. It plays a crucial role in every programming concept like C, C++, Java, etc.

The operators are used to solve logical and mathematical problems. For example, to calculate the temperature given by the sensor based on some analog voltage.

The types of Operators classified in Arduino are:

1. Arithmetic Operators
2. Compound Operators
3. Boolean Operators
4. Comparison Operators
5. Bitwise Operators

### Arithmetic Operators

There are six basic operators responsible for performing mathematical operations in Arduino, which are listed below:

• Assignment Operator ( = )

The Assignment operator in Arduino is used to set the variable's value. It is quite different from the equal symbol (=) normally used in mathematics.

• Addition ( + )

The addition operator is used for the addition of two numbers. For example, P + Q.

• Subtraction ( - )

Subtraction is used to subtract one value from the another. For example, P - Q.

• Multiplication ( * )

The multiplication is used to multiply two numbers. For example, P * Q.

• Division ( / )

The division is used to determine the result of one number divided with another. For example, P/Q.

• Modulo ( % )

The Modulo operator is used to calculate the remainder after the division of one number by another number.

Most of the operators are similar to the usual operator used in mathematics.

Let's understand the operators with the help of two examples.

Example 1:

Consider the below code.

In the above code, we have assigned the result of the addition of two numbers to b before printing it to the console.

For output, click on the Upload and Serial Monitor button present on the toolbar.

Output: 7

Example 2:

Consider the below code:

Here, d= d +3 is not operated as a usual mathematical operation. It is the assignment operator where right of the function is evaluated first and is assigned to the left of the equal sign.

Let's consider the below image for better understanding. Output:

```3
6
```

Similarly, we can perform multiplication, modulo, and division. The int variable will store the integer values. For example, 20/3 = 6.

If we want decimal values to be printed, we need to use the float instead of int.

For example,

Consider the below code:

Output: 6.66

### Order of mathematical operations

Let's understand the order of operations considered by the Arduino while performing calculation:

1. Parentheses ( )
2. Multiplication, division, and modulo
3. Addition and subtraction

If there are multiple operations next to each other, they will be computed from left to right.

Let's understand with an example.

Consider the below code:

Output:

```6
```

Let's understand how the above output occurred. Consider the below image: ### Compound Operators

The compound operators perform two or more calculations at once.

The result of the right operand is assigned to the left operand, as already discussed above. The same condition will apply to all the compound operators, which are listed below:

Let's consider a variable b.

• b + +

Here, b = b + 1. It is called the increment operator.

• b + =

For example, b + = 4. It means, b = b+ 4.

• b - -

Here, b = b - 1. It is called as the decrement operator.

• b - =

For example, b - = 3. It means, b = b - 3.

• b * =

For example, b * = 6. It means, b = b * 6.

• b / =

For example, b / = 5. It means, b = b / 5.

• b % =

For example, b % = 2. It means, b = b % 2.

Now, let's use the above operators with two variables, b and c.

• b + = c ( b = b + c)
• b - = c ( b = b - c)
• b * = c ( b = b * c)
• b / = c ( b = b / c)
• b % = c ( b = b % c)

We can specify any variable instead of b and c.

### Boolean Operators

The Boolean Operators are NOT ( ! ), Logical AND ( & & ), and Logical OR ( | | ).

Let's discuss the above operators in detail.

• Logical AND ( & & )

The result of the condition is true if both the operands in the condition are true.

Consider the below example:

Above statement is true if both conditions are true. If any of the conditions is false, the statement will be false.

• Logical OR ( | | )

The result of the condition is true, if either of the variables in the condition is true.

Consider the below example.

The above statement is true, if either of the above condition ( a> 0 or b > 0 ) is true.

• NOT ( ! )

It is used to reverse the logical state of the operand.

For example, a ! = 2.

The NOT operator returns the value 1 or TRUE when the specified operand is FALSE. It also reverses the value of the specified expression.

## Comparison Operators

The comparison operators are used to compare the value of one variable with the other.

The comparison operators are listed below:

• less than ( < )

The less than operator checks that the value of the left operand is less than the right operand. The statement is true if the condition is satisfied.

Consider the below code.

Output: 3

In the above code, if any of the two statement is correct, the corresponding value of the variable will be printed. Here, only first condition is correct. Hence, the value of b will be printed.

• greater than ( > )

The less than operator checks that the value of the left side of a statement is greater than the right side. The statement is true if the condition is satisfied.

For example, a > b.

If a is greater than b, the condition is true, else false.

• equal to ( = = )

It checks the value of two operands. If the values are equal, the condition is satisfied.

For example, a = = b.

The above statement is used to check if the value of a is equal to b or not.

• not equal to ( ! = )

It checks the value of two specified variables. If the values are not equal, the condition will be correct and satisfied.

For example, a ! = b.

• less than or equal to ( < = )

The less or equal than operator checks that the value of left side of a statement is less or equal to the value on right side. The statement is true if either of the condition is satisfied.

For example, a < = b

It checks the value of a is less or equal than b.

• greater than or equal to ( > = )

The greater or equal than operator checks that the value of the left side of a statement is greater or equal to the value on the right side of that statement. The statement is true if the condition is satisfied.

For example, a > = b

It checks the value of a is greater or equal than b. If either of the condition satisfies, the statement is true.

## Bitwise Operators

The Bitwise operators operate at the binary level. These operators are quite easy to use.

There are various bitwise operators. Some of the popular operators are listed below:

• bitwise NOT ( ~ )

The bitwise NOT operator acts as a complement for reversing the bits.

For example, if b = 1, the NOT operator will make the value of b = 0.

Let's understand with another example.

• bitwise XOR ( ^ )

The output is 0 if both the inputs are same, and it is 1 if the two input bits are different.

For example,

For example,

• bitwise OR ( | )

The output is 0 if both of the inputs in the OR operation are 0. Otherwise, the output is 1. The two input patterns are of 4 bits.

For example,

• bitwise AND ( & )

The output is 1 if both the inputs in the AND operation are 1. Otherwise, the output is 0. The two input patterns are of 4 bits.

For example,

• bitwise left shift ( < < )

The left operator is shifted by the number of bits defined by the right operator.

• bitwise right shift ( > > )

The right operator is shifted by the number of bits defined by the left operator.

Next TopicArduino array

## Help Others, Please Share   