Compound operators are a concise way of performing arithmetic or logical operations in programming. In Arduino programming, these operators allow you to perform an operation and assign the result to the variable in a single step, making the code more efficient and easier to read.  

In this article you will learn different types of compound operators that are commonly used in Arduino programming.

Operator nameOperator symbolDescriptionExample
Increment++Increases integer value by 1.a++ will give 6
Decrement--Decreases integer value by 1.a-- will give 4
Compound addition+=Adds the right operand to the left operand and assigns the result to left operand.b += a is equivalent to b = b + a will give 15
Compound subtraction-=Subtracts the right operand from the left operand and assigns the result to left operand.b -= a is equivalent to b = b – a will give 5
Compound multiplication*=Multiplies the right operand with the left operand and assigns the result to left operand.b*= a is equivalent to b = b* a will give 50
Compound division/=Divide the left operand with the right operand and assign the result to the left operand.b /= a is equivalent to b = b / a
will give 2
Compound modulo%=It takes modulus using two operands and assigns the result to the left operand.b %= a is equivalent to b = b % a
will give 0
Compound bitwise OR|=Bitwise inclusive OR and assignment operator.a |= 2 is same as a = a | 2 will give 7
Compound bitwise AND&=Bitwise AND assignment operator.a &= 2 is same as a = a & 2 will give 0

Increment Operator

In Arduino, the increment operator is denoted by ++’, and is used to increase the value of a variable by one. It is a simple and efficient way to perform incremental counting and iterative tasks in Arduino programming.

There are two ways to use the increment operator in Arduino:

Post-increment (variable++): The value of the variable is first used in the expression, and then its value is incremented by one.

Pre-increment (++variable): The value of the variable is incremented by one before it is used in the expression.

Here’s a simple example using the increment operator in an Arduino sketch:

void setup() {
Serial.begin(9600);
  int a = 1;
  int b = 1;
  int c = 1 + (a++);
  int d = 1 + (++b);
  Serial.print("\nThe Value of variable c =");
  Serial.print(c);
  Serial.print("\nThe Value of variable d =");
  Serial.print(d);
}

void loop(){

}

Here we have two int type variables called a and b both of them are set equal to 1. Then we have another int variable called c and d to save the result of post increment and pre-increment operation. If we run this code,

Serial Output Will be:

 The value of variable c =2

The value of variable d =3

In the first expression c = 1 + (a++) . The value of variable a is used first and increment in value of variable a is performed later. Hence c = 1 + 1 = 2.

In the second expression d = 1 + (++b) first increment in the value of variable b occurs and then it is used , hence d = 1 + 2 = 3.

Decrement Operator

In the Arduino IDE, the decrement operator is denoted by ‘–‘. It subtracts 1 from the value of a variable. It is used to decrease the value of a variable by 1, and it can be applied to various numeric data types, such as int, long, byte, etc.

The decrement operator can be used in two ways:

Post-decrement(variable–): When the ‘–‘ operator is placed after the variable, it returns the current value of the variable and then decrements it. 

Pre-decrement(–variable): When the ‘–‘ operator is placed before the variable, it first decrements the value of the variable and then returns the updated value.

For example, take a look at the sketch using the decrement operator:

void setup() {
Serial.begin(9600);
  int a = 2;
  int b = 2;
  int c = 1 + (a--);
  int d = 1 + (--b);
  Serial.print("\nThe Value of variable c =");
  Serial.print(c);
  Serial.print("\nThe Value of variable d =");
  Serial.print(d);
}

void loop(){

}

Here we have two int type variables called a and b both of them are set equal to 2. Then we have another int variable called c and d to save the result of post-decrement and pre-decrement operation. If we run this code,

Serial Output Will be:

 The value of variable c =3

The value of variable d =2

In First Expression c = 1 + (a) . The value of variable a is used first and decrement in value of variable a is performed later. Hence c = 1 + 2 = 3.

In Second Expression d = 1 +(–b) first decrement in the value of variable b occurs and then it is used , hence d = 1 + 1 = 2.

Here, a is set equal to 1, so b will equal to 2 since 1 + 1 = 1. The math is performed before the variable is incremented.

It’s important to use the decrement operator with caution, as improper usage can lead to unexpected results, especially when combined with other operations or used in complex expressions. Always make sure to understand how the operator behaves and ensure it’s used correctly in your code.

Compound Addition Operator

In the Arduino IDE, the compound addition operator is denoted by ‘+=’. It takes a variable and adds another variable to it, then stores the result in the first variable. 

For example, take a look at the following sketch:

void setup() {
  Serial.begin(9600);
  int a = 3;
  int b = 5;
  a += b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a += b to add the b variable to the a variable. Hence actual operation will be like this

a = a + b     this implies  a = 3 + 5 = 8.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The value of variable a = 8

Compound Subtraction Operator

The compound subtraction operator is denoted by ‘-=‘. It takes a variable and subtracts another variable from it. The result is stored in the first variable. Compound subtraction is written like this:

a -= b;

For example, take a look at the following sketch:

void setup() {
  Serial.begin(9600);
  int a = 5;
  int b = 2;
  a -= b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a -= b to subtract the b variable from a variable. Hence actual operation will be like this

a = ab     this implies  a = 5 – 2 = 3.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The value of variable a = 3

Compound Modulo Operator

In Arduino programming, the compound modulo operator (%=) is used to perform the modulo operation and update the value of a variable in a single step. The modulo operation calculates the remainder when one number is divided by another.

The syntax of the compound modulo operator is as follows:

variable %= divisor;

Let’s take an example:

void setup() {
  Serial.begin(9600);
  int a = 5;
  int b = 2;
  a %= b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a %= b to find the modulus when b variable is divided by a variable. Hence actual operation will be like this:

a = a % b     this implies  a = 5 % 2 = 1.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The Value of variable a = 1

Compound Multiplication Operator

The compound multiplication operator is written as the asterisk with equal to operators *=’. It is used to multiply a variable by another variable and stores the result in any of the two variables.

For example, there are two variables a and b of data type integer and in Arduino programming if we have to multiply them using compound multiplication operator. The output will get stored in the variable a. Here the value of a is 5 and the value for b is 2 so the result for multiplication will be 10:

void setup() {
  Serial.begin(9600);
  int a = 5;
  int b = 2;
  a *= b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a *= b to find the result when b variable is multiplied by a variable. Hence actual operation will be like this

a = a * b     this implies  a = 5 * 2 = 10.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The value of variable a = 10

Compound Division Operator 

The compound division operator is written with assignment operator (=) and the division operator (/). It is denoted by ‘/=’.

a /= b

After the division is performed the result is stored in a variable on the left.

For example, 

void setup() {
  Serial.begin(9600);
  int a = 5;
  int b = 2;
  a /= b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a /= b to find the result when b variable is divided by a variable. Hence, actual operation will be like this:

a = a / b     this implies  a = 5 / 2 = 2.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The Value of variable a = 2

Compound Bitwise OR Operator 

This compound operator is represented by using ‘|=’.

a |= b; is equivalent to writing a = a | b;

That is, the value of a after the line will be equal to its old value bitwise ORed with the value of b.

Let’s first understand the Bitwise OR (|) operator:

0  0  1  1    operand1 (3)

0  1  0  1    operand2 (5)

———-

0  1  1  1    (operand1 | operand2) = result (7)

Bits that are “bitwise ORed” with 0 are unchanged, while bits that are “bitwise ORed” with 1 are set to 1.

Suppose if the value of a variable a is 3 and its binary is (0011) and similarly the value for a variable b is 5 and its binary is (0101) and if the compound bitwise OR operator is used then it will apply OR operation on each bit of a and b. The output of the OR operator will only be zero if both the bits are zero. In our case the result of the compound OR operation will be (0111) that is 7.

void setup() {
  Serial.begin(9600);
  int a = 3;
  int b = 5;
  a |= b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a |= b to find the result when b variable is bitwise ORed with a variable. Hence actual operation will be like this

a = a | b     this implies  a =  3 | 5 = 7.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The value of variable a = 7

Compound Bitwise AND Operator

Another Boolean operator that can be used in Arduino programs to perform logical operation is the AND operation. Combining both the AND operator and assignment operator form a compound bitwise AND operator. It is denoted by ‘&=’.

In Arduino programming, a &= b; is equivalent to a = a & b;

That is, the value of a after the line will be equal to its old value. 

Let’s first understand the Bitwise AND (&) operator

0  0  1  1    operand1 (3)

0  1  0  1    operand2 (5)

———-

0  0  0  1    (operand1 & operand2) = result (1)

As shown above, bits that are “bitwise ANDed” with 0 become 0, while bits that are “bitwise ANDed” with 1 are left unchanged.

If the compound AND operator is applied on the variables a = 3 having binary (0011) and b = 5 having binary (0101) .The result will be (0001) that is 1.

void setup() {
  Serial.begin(9600);
  int a = 3;
  int b = 5;
  a &= b;
  Serial.print("\nThe Value of variable a = ");
  Serial.print(a);
}

void loop() {

}

In the code above, we use the compound addition operator a &= b to find the result when b variable is bitwise ANDed with a variable. Hence actual operation will be like this

a = a & b     this implies a = 3 & 5 = 1.

The value stored in a is then printed to the serial monitor. If we run this code,

Serial Output Will be:

The value of variable a = 1