# Arduino Operators: Arithmetic, Comparison, Boolean, Bitwise, and Compound

## Introduction

Arduino is an open-source electronics platform that allows users to create interactive projects. One of the key aspects of programming in Arduino is the use of operators. Operators are symbols or keywords that perform specific operations on variables or values. In this article, we will explore the different types of operators in Arduino, including arithmetic, comparison, boolean, bitwise, and compound operators.

**Arithmetic Operators:**

Arithmetic operators are used to perform mathematical calculations in Arduino. These operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). Addition is used to add two values together, subtraction is used to subtract one value from another, multiplication is used to multiply two values, division is used to divide one value by another, and modulus is used to find the remainder of a division operation.

**Comparison Operators:**

Comparison operators are used to compare two values in Arduino. These operators include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). These operators return a boolean value (true or false) based on the comparison result.

**Boolean Operators:**

Boolean operators are used to perform logical operations in Arduino. These operators include logical AND (&&), logical OR (||), and logical NOT (!). Logical AND returns true if both operands are true, logical OR returns true if either operand is true, and logical NOT returns the opposite boolean value of the operand.

**Bitwise Operators:**

Bitwise operators are used to perform operations on individual bits in Arduino. These operators include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>). These operators are useful when working with binary values or manipulating individual bits in a variable.

**Compound Operators:**

Compound operators are used to combine a simple assignment with an arithmetic or bitwise operation in Arduino. These operators include addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), division assignment (/=), modulus assignment (%=), bitwise AND assignment (&=), bitwise OR assignment (|=), bitwise XOR assignment (^=), left shift assignment (<<=), and right shift assignment (>>=). These operators provide a concise way to update the value of a variable based on a specific operation.

In the following sections, we will dive deeper into each type of operator and explore their usage and examples in Arduino programming.

## Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations on numerical operands. They include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators allow you to add, subtract, multiply, divide, and find the remainder of two numbers, respectively.

For example, if you want to add two variables, you can use the addition operator:

int a = 5;

int b = 3;

int sum = a + b;

In this case, the variable “sum” will hold the value 8, which is the result of adding the values of “a” and “b”.

Similarly, you can use other arithmetic operators to perform different mathematical operations based on your requirements.

Subtraction is another arithmetic operator that allows you to subtract one number from another. For example:

int x = 10;

int y = 7;

int difference = x – y;

In this case, the variable “difference” will hold the value 3, which is the result of subtracting the value of “y” from “x”.

Multiplication is another arithmetic operator that allows you to multiply two numbers together. For example:

int m = 4;

int n = 6;

int product = m * n;

In this case, the variable “product” will hold the value 24, which is the result of multiplying the values of “m” and “n”.

Division is another arithmetic operator that allows you to divide one number by another. For example:

int p = 20;

int q = 5;

int quotient = p / q;

In this case, the variable “quotient” will hold the value 4, which is the result of dividing the value of “p” by “q”.

Modulus is another arithmetic operator that allows you to find the remainder of one number divided by another. For example:

int r = 17;

int s = 5;

int remainder = r % s;

In this case, the variable “remainder” will hold the value 2, which is the remainder when “r” is divided by “s”.

These arithmetic operators are fundamental in performing mathematical calculations in programming and are used extensively in various applications.

## Comparison Operators

Comparison operators are used to compare values and evaluate conditions. They return a boolean value (true or false) based on the comparison result. The comparison operators available in Arduino programming are:

Equal to (==)

Not equal to (!=)

Greater than (>)

Less than (<)

Greater than or equal to (>=)

Less than or equal to (<=)

These operators are commonly used in conditional statements, loops, and other control structures to make decisions based on the comparison results.

For example, let’s say you have a variable “temperature” and you want to check if it is greater than a certain threshold:

int temperature = 25;

int threshold = 30;

if (temperature > threshold) {

Â // Do something if the temperature is greater than the threshold

}

In this case, the condition “temperature > threshold” will be evaluated, and if it is true, the code inside the if statement will be executed.

Comparison operators are not limited to just numbers. They can also be used to compare other data types such as strings. For example, you can use the equal to operator (==) to check if two strings are equal:

String name1 = “Fawad khan”;

String name2 = “Fawad khan”;

if (name1 == name2) {

Â // Do something if the names are equal

}

In this case, the condition “name1 == name2” will be evaluated, and if it is true, the code inside the if statement will be executed.

Comparison operators can also be used in combination with logical operators to create more complex conditions. For example, you can use the greater than or equal to operator (>=) along with the logical AND operator (&&) to check if a number is within a certain range:

int number = 10;

int lowerBound = 5;

int upperBound = 15;

if (number >= lowerBound && number <= upperBound) {

Â // Do something if the number is within the range

}

In this case, the condition “number >= lowerBound && number <= upperBound” will be evaluated, and if it is true, the code inside the if statement will be executed.

It is important to note that comparison operators have a specific order of precedence, which means that some operators are evaluated before others. For example, the greater than operator (>) is evaluated before the equal to operator (==). If you want to change the order of evaluation, you can use parentheses to group the operators. For example:

int a = 5;

int b = 10;

int c = 15;

if ((a + b) > c) {

Â // Do something if the sum of a and b is greater than c

}

In this case, the condition “(a + b) > c” will be evaluated, and if it is true, the code inside the if statement will be executed.

Overall, comparison operators are essential tools in Arduino programming for making decisions based on the comparison of values. By understanding how these operators work and how to use them effectively, you can create more dynamic and responsive programs.

## Boolean Operators

Boolean operators are used to perform logical operations on boolean values (true or false). The boolean operators available in Arduino programming are:

Logical AND (&&)

Logical OR (||)

Logical NOT (!)

These operators are commonly used to combine multiple conditions or negate a condition.

For example, let’s say you have two conditions “condition1” and “condition2”, and you want to check if both conditions are true:

bool condition1 = true;

bool condition2 = false;

if (condition1 && condition2) {

Â // Do something if both conditions are true

}

In this case, the condition “condition1 && condition2” will be evaluated, and if both conditions are true, the code inside the if statement will be executed.

Similarly, you can use the logical OR operator to check if at least one of the conditions is true:

bool condition1 = true;

bool condition2 = false;

if (condition1 || condition2) {

Â // Do something if at least one condition is true

}

In this case, the condition “condition1 || condition2” will be evaluated, and if either condition1 or condition2 is true, the code inside the if statement will be executed.

The logical NOT operator can be used to negate a condition. For example:

bool condition1 = true;

if (!condition1) {

Â // Do something if condition1 is false

}

In this case, the condition “!condition1” will be evaluated, and if condition1 is false, the code inside the if statement will be executed.

By using these boolean operators, you can create complex conditions and control the flow of your Arduino program based on the evaluation of these conditions.

## Bitwise Operators

Bitwise operators are used to perform operations on individual bits of a value. Arduino supports the following bitwise operators:

Bitwise AND (&): This operator performs a bitwise AND operation on the binary representation of two values. For example, int result = 5 & 3; will assign the value 1 to the variable result.

Bitwise OR (|): This operator performs a bitwise OR operation on the binary representation of two values. For example, int result = 5 | 3; will assign the value 7 to the variable result.

Bitwise XOR (^): This operator performs a bitwise XOR (exclusive OR) operation on the binary representation of two values. For example, int result = 5 ^ 3; will assign the value 6 to the variable result.

Bitwise NOT (~): This operator performs a bitwise NOT operation on the binary representation of a value. For example, int result = ~5; will assign the value -6 to the variable result.

Left shift (<<): This operator shifts the bits of a value to the left by a specified number of positions. For example, int result = 5 << 2; will assign the value 20 to the variable result.

Right shift (>>): This operator shifts the bits of a value to the right by a specified number of positions. For example, int result = 5 >> 2; will assign the value 1 to the variable result.

Bitwise AND (&): This operator performs a bitwise AND operation on the binary representation of two values. It returns a value where each bit is set to 1 only if both corresponding bits in the operands are 1. For example, if we have two values: 5 (binary: 101) and 3 (binary: 011), the bitwise AND operation will result in 1 (binary: 001).

Bitwise operators are useful when working with binary data or when performing low-level operations on individual bits. They allow you to manipulate and extract specific bits from a binary value, which can be particularly useful in applications such as data compression, encryption, and device control.

## Compound Operators

Compound operators, also known as shorthand operators, are used to perform an operation and assign the result to the same variable in a single statement. They provide a concise way to update the value of a variable based on a specific operation.

The compound operators available in Arduino programming are:

Addition and assignment (+=)

Subtraction and assignment (-=)

Multiplication and assignment (*=)

Division and assignment (/=)

Modulus and assignment (%=)

Bitwise AND and assignment (&=)

Bitwise OR and assignment (|=)

Bitwise XOR and assignment (^=)

Left shift and assignment (<<=)

Right shift and assignment (>>=)

These operators allow you to perform an operation and update the value of a variable in a single step.

For example, let’s say you have a variable “counter” and you want to increment it by 1:

int counter = 0;

counter += 1;

In this case, the compound addition and assignment operator “+=” is used to increment the value of “counter” by 1. The statement “counter += 1” is equivalent to “counter = counter + 1”.

Compound operators are not only limited to arithmetic operations. They can also be used with bitwise operations, such as bitwise AND, OR, XOR, and shift operations.

For example, consider the following code:

int a = 5;

a &= 3;

In this code, the compound bitwise AND operator “&=” is used to perform a bitwise AND operation between the values of “a” and 3, and assign the result back to “a”. The statement “a &= 3” is equivalent to “a = a & 3”.

Similarly, you can use compound operators with other bitwise operations like bitwise OR, XOR, and shift operations.

Compound operators are particularly useful when you want to update the value of a variable based on a specific operation without writing multiple lines of code. They provide a more concise and efficient way to perform such operations.