Arduino

Arduino Compound Operators

To determine the flow of a program or a code in Arduino Programming the compiler should understand how to perform multiple operations which can be either mathematical or logical. For this, different types of operators are used. Here in this article the compound operators will be briefly discussed.

What are compound operators in Arduino

In Arduino programming the operators are very important as they influence the output of a program. The word compound means a composite of two or more things. Similarly, the compound operators are a composite of two operators. In other words, to perform two operations in one statement a compound operator is used. There are basically nine types of compound operators that are discussed in detail.

Compound multiplication operator (*=)

This operator has two operators one is multiplication and another is operator used for assigning any value to any variable or constant. the compound multiplication operator can used by using the asterisk with equal to operators “*=”. To store the result of operation no third variable is required .The result can be stored in any of the two variables that are to be multiplied.

For instance, if we suppose 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 4 and the value for B is 2 so the result for multiplication will be 8:

int a = 4;
int b = 2
void setup() {
   Serial.begin(9600);
   Serial.print("Compound multiplication operator:");
   a *= b;
   Serial.println(a);
}
void loop(){
}

Output

Compound Division Operator (/=)

The compound division operator is a combination of an assignment operator (=) and the division operator (/). This operator in the Arduino Programming can be used by using forward slash with equals to operators “/=”. After the division is performed the result is stored in a variable on the left.

For example, if two variables having either float data type or integer data type are to be divided using compound division operator. Suppose the variable A has a value of 4 and the variable B has a value of 2 the answer for the division will be 2.

int a = 4;
int b = 2;
void setup() {
   Serial.begin(9600);
   Serial.print("Compound division operator:");
   a /= b;
 Serial.println(a);
}
void loop(){
}

Output

Compound Addition Operator (+=)

To perform the mathematical operation of sum and to assign the result to one of the variables the compound addition operator is used. This operator is represented by (+=). Suppose there are two variables A , B and the compound addition operator is to be applied using Arduino programing. For instance, if variable A is given a value of 4 and variable B is assigned value 2 the result of applying addition operation will be 6.

int a = 4;
int b = 2;
void setup() {
   Serial.begin(9600);
   Serial.print("Compound addition operator:");
 a += b;
Serial.println(a);
}
void loop(){
}

Output

Compound Subtraction Operator (-=)

When the subtraction of two variables is to be needed in Arduino programming, the subtraction operator and the assigning operator are used to store the result of the subtraction operator. Together they make compound subtraction operators and can be used by using the “-=” sign. To further Suppose the value of variable A is 4 and the value of variable B is 2 and compound subtraction operator is applied in Arduino programming the result will be 2:

int a = 4;
int b = 2;
void setup() {
   Serial.begin(9600);
   Serial.print("Compound division operator:");
        a -= b;
   Serial.println(a);
}
void loop(){
}

Output

Increment (++) and Decrement (–) operators

The incremental operator increases the value by one however the decrement operator decreases the value of any variable by one. The incremental operator can be represented by the “++” sign whereas the decrement operator can be used by using the “–“ sign.

For example, suppose a variable i has a value of 1 and the value of j is 2. The increment operator is applied on the variable i and the output will be 2. However the decrement operator is applied on the variable j and the result of decrement will be 1:

int i = 1;
int j = 2;
void setup() {
   Serial.begin(9600);
   Serial.print("Compound increment operator:");
    i++;
Serial.println(i++);
 Serial.print("Compound decrement  operator:");
    j--;
Serial.println(j--);
}
void loop(){
}

Output

Compound Modulo Operator (%=)

To find the remainder when two numbers that are not equally divided in Arduino programming and to assign the answer to one of the variables the compound modulo operator is used. To use this operator the sign “%=” is used. To further explain the use of compound modulo operator in Arduino we can take an example; suppose the value of a variable A is 4 and value for B is 3 as they are not completely divisible so the remainder obtained by the modulo operation will be 1:

int a = 4;
int b = 3;
void setup() {
   Serial.begin(9600);
   Serial.print("Compound Modulo operator:");
   a %= b;
   Serial.println(a);  
}
void loop() {
}

Output

Compound Bitwise OR Operator (|=)

This compound operator is represented by using “|=”. This operator first converts the integers to the binary form and operates bit by bit on the binary of the converted integer.

Suppose if the value of variable A is 4 and its binary is (0100) and similarly the value for variable B is 2 and its binary is (0010) 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 outputs are zero. In this case the result of the compound OR operation will be (0110) that is 6:

int a = 4;
int b = 2;
void setup() {
   Serial.begin(9600);
     Serial.print("Compound Bitwise OR  operator:");
 a |= b;
   Serial.println(a);
}
void loop() {
}

Output

Compound Bitwise AND Operator (&=)

Another Boolean operator that can be used in Arduino programming to perform logical operation is the AND operation. Another operator used with the AND operator is the assigning operator. Combining both the AND operator and assignment operator form a compound bitwise AND operator. To use this compound operator in Arduino programming the sign used is “&=”. The AND operator like the OR operator also operates bit by bit. However, the output of AND operator is one only if both inputs are one and vice versa.

For example, if the compound AND operator is applied on the variables a and b having binaries of (0100) for 4 and (0010) for 2 the result will be (0000) that is 0.Similary two other variables c and d having values of 5 (0101) and 7 (0111) are supposed and the AND operation is applied on the two values the result for the c and d is 5 (0101).

int a = 4;
int b = 2;
int c= 5;
int d= 7;
void setup() {
   Serial.begin(9600);
        Serial.print("Compound Bitwise AND  operator on a and b  :");
    a &= b;
   
   Serial.println(a);
   Serial.print("Compound Bitwise AND  operator on c and d :");
   c &= d;
   Serial.println(c);
}
void loop() {
}

Output

Conclusion

A compound operator used in Arduino programming is composed of two operators, one the assigned operator and the other is the either Boolean operator or any mathematical operator. The significance of compound operators is that they are used for logical and arithmetic operations required in Arduino programming and also the result is stored to the left operand directly by using the assign function. In this article nine different types of compound operators that are commonly used in Arduino programming are briefly discussed and to give a clear idea of usage of these operators with examples.

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.