Skip to content

Arduino - Operators

Operators are special symbols that instruct the compiler to perform specific mathematical, relational, or logical operations. Since Arduino programming is based on C++, it uses the standard C++ operators. This guide provides practical, runnable examples for each type.

To run these examples: Copy the code into the Arduino IDE, upload it to your board, and open the Serial Monitor (Tools > Serial Monitor) with the baud rate set to 9600.

These are used for performing mathematical calculations.

OperatorNameDescription
=AssignmentAssigns the value on the right to the variable on the left.
+AdditionAdds two operands.
-SubtractionSubtracts the second operand from the first.
*MultiplicationMultiplies two operands.
/DivisionDivides the first operand by the second.
%ModuloReturns the remainder of an integer division.
void setup() {
Serial.begin(9600);
int a = 10;
int b = 3;
Serial.print("a = 10, b = 3\n");
Serial.print("a + b = "); Serial.println(a + b); // Result: 13
Serial.print("a - b = "); Serial.println(a - b); // Result: 7
Serial.print("a * b = "); Serial.println(a * b); // Result: 30
Serial.print("a / b = "); Serial.println(a / b); // Result: 3 (Integer division, remainder is discarded)
Serial.print("a % b = "); Serial.println(a % b); // Result: 1 (Remainder of 10/3)
// For floating-point division:
float x = 10.0;
float y = 3.0;
Serial.print("x / y = "); Serial.println(x / y); // Result: 3.33
}
void loop() {}

These operators compare two values and return a boolean result (true or false). They are most often used in conditional statements like if.

OperatorNameDescription
==Equal toReturns true if operands are equal.
!=Not equal toReturns true if operands are not equal.
<Less thanReturns true if the left operand is less than the right.
>Greater thanReturns true if the left operand is greater than the right.
<=Less than or equal toReturns true if the left operand is less than or equal to the right.
>=Greater than or equal toReturns true if the left operand is greater than or equal to the right.
void setup() {
Serial.begin(9600);
int sensorValue = 750;
int threshold = 500;
Serial.print("Sensor Value: "); Serial.println(sensorValue);
Serial.print("Threshold: "); Serial.println(threshold);
if (sensorValue > threshold) {
Serial.println("Result: Sensor value is greater than threshold.");
}
if (sensorValue == 750) {
Serial.println("Result: Sensor value is exactly 750.");
}
}
void loop() {}

These operators are used to combine multiple conditional expressions.

OperatorNameDescription
&&Logical ANDReturns true only if both operands are true.
||Logical ORReturns true if at least one of the operands is true.
!Logical NOTInverts the boolean value of its operand (true becomes false, false becomes true).
void setup() {
Serial.begin(9600);
int temperature = 25;
int humidity = 50;
// Example 1: AND
if (temperature >= 20 && humidity < 60) {
Serial.println("Condition 1: Temperature is pleasant AND humidity is normal.");
}
// Example 2: OR
if (temperature > 30 || humidity > 80) {
Serial.println("Condition 2: It's either too hot OR too humid.");
}
// Example 3: NOT
bool isRaining = false;
if (!isRaining) {
Serial.println("Condition 3: It is NOT raining.");
}
}
void loop() {}

Bitwise operators work on the individual bits of integer data types. They are highly efficient and commonly used in low-level microcontroller programming to manipulate hardware registers.

OperatorNameDescription
&Bitwise ANDSets each bit to 1 if both corresponding bits are 1.
|Bitwise ORSets each bit to 1 if at least one of the corresponding bits is 1.
^Bitwise XORSets each bit to 1 if only one of the corresponding bits is 1.
~Bitwise NOTInverts all the bits.
<<Left ShiftShifts bits to the left, filling with zeros. (Fast multiplication by 2).
>>Right ShiftShifts bits to the right. (Fast integer division by 2).
// A byte is 8 bits. B00000001 is the binary representation for the number 1.
byte flags = B00000001;
void setup() {
Serial.begin(9600);
// Use Case 1: Set a bit (turn a flag on) with OR
// Let's set the 3rd bit (bit 2)
flags = flags | B00000100; // Result: B00000101
Serial.print("After setting bit 2: "); Serial.println(flags, BIN);
// Use Case 2: Check a bit with AND
// Is bit 2 still set?
if ((flags & B00000100) > 0) {
Serial.println("Bit 2 is ON.");
}
// Use Case 3: Toggle a bit with XOR
// Let's toggle the 1st bit (bit 0)
flags = flags ^ B00000001; // Result: B00000100
Serial.print("After toggling bit 0: "); Serial.println(flags, BIN);
// Use Case 4: Shift Left
byte val = B00001100; // 12
Serial.print("12 << 1 = "); Serial.println(val << 1); // 24
}
void loop() {}

Compound operators are a shorthand for performing an operation on a variable and assigning the new value back to the same variable.

ShorthandEquivalent ToName
x++x = x + 1Increment
x—x = x - 1Decrement
x += yx = x + yCompound Addition
x -= yx = x - yCompound Subtraction
x *= yx = x * yCompound Multiplication
x /= yx = x / yCompound Division
void setup() {
Serial.begin(9600);
int counter = 0;
counter++; // counter is now 1
Serial.print("Counter after ++: "); Serial.println(counter);
int score = 100;
score -= 20; // score is now 80
Serial.print("Score after -= 20: "); Serial.println(score);
}
void loop() {}