Arduino – Operators

An operator is a symbol that instructs the compiler to perform certain logical or mathematical tasks. Take them in order.

Mathematical operators

The assignment operator stores the value to the right of the equal sign in the variable to the left of the equal sign. Not to be confused with the equal comparison operator “==”.

A = B;
int a = 10;

The values ​​of the operands “X” and “Y” (these can be variables or constants) are added and passed to the variable “A”. Syntax of the addition operator.

A = X + Y;

The subtraction operator, I think does not need to be explained.

A = X - Y;

The multiplication.

A = X * Y;

And the division.

A = X / Y;

The variable “X” is divided by the variable “Y” and the remainder of the division is passed to the variable “A”. The data type of the variable “A” can be int, float or double, “X” and “Y” are int only. Syntax of the remainder operator:

A = X % Y;
int a = 7 % 5;  // a --> 2

Comparative operators

The equal to operator (double equal sign) “==” checks whether the values ​​of two operands are equal or not, if so, the condition will be true. If the value of “A” is the same as the value of “B” then it will be true, otherwise it is false.

int A = 12;
int B = 15;
if(A == B)  // --> false

The not equal to operator “! =” Checks whether the values ​​of two operands are equal or not, if the values ​​are not equal, the condition will be true. Let’s look at an example.

int A = 12;
int B = 15;
if(A != B)  // --> true

Less than operator “<” compares the variable on the left with the value or variable on the right of the operator. The condition will be true if the operand on the left is smaller than the operand on the right, otherwise false.

int A = 12;
int B = 15;
if(A < B)  // --> true

Greater than operator “>” compares the variable on the left with the value or variable on the right of the operator. The condition will be true if the operand on the left is larger than the operand on the right, otherwise false.

int A = 12;
int B = 15;
if(A > B)  // --> false

The less than or equal to operator “<=” compares the variable on the left with the value or variable on the right of the operator. The condition will be true if the left operand is less than or equal to the right operand, otherwise false.

int A = 12;
int B = 15;
if(A <= B)  // --> true

The greater than or equal to operator ”> =” compares the variable on the left with the value or variable on the right of the operator. The condition will be true if the operand on the left is larger than the operand on the right, otherwise false.

int A = 12;
int B = 15;
if(A >= B)  // --> false

Logical operators

Both operands of the logical and “&&” operators are true, then the condition will be true, otherwise false.

int A = 12;
int B = 15;
if(A == 15  && B == 15)  // --> false

The logical or “||” if at least one operand of the operator is true, then the condition will be true.

int A = 12;
int B = 15;
if(A == 15  || B == 15)  // --> true

The logical not is “!” operator reverses the logical state of the operand.

bool A = true;
if(!A)  // --> false

Bitwise operators

The binary “and” “&” operators work independently at each bit position in the byte. According to this rule: if both input bits are 1, then the output obtained is 1, otherwise the output is 0.

0 0 1 1  // operand1
0 1 0 1  // operand2
-------
0 0 0 1  // (operand1 & operand2)

The binary “or” “|” operator operates independently on each bit position of an expression. If either one input bit has a value of 1 or the other input bit has a value of 1 or both, the output will be 1. If both input bits are set to 0, the output will be 0.

0 0 1 1 // operandus1
0 1 0 1 // operandus2
-------
0 1 1 1 // (operand1 | operand2)

The binary “exclusive or” operator is “^”. The result of the bitwise XOR operation is 1 if the input bits are different, otherwise 0.

0  0  1  1   // operand1 
0  1  0  1   // operand2 
----------
0  1  1  0   // (operand1 ^ operand2)

The bitwise “not” operator “~” inverts the bits. 1 will become 0.

0 0 1 1  // operand1
-------
1 1 0 0  // ~ operand1

The “bitshift left” operator is “<<“. Move the value of operand “B” to the left with position “C”. The left bits of operand “B” are extended and lost.

A = B << C;

int B = 5;      // binary: 0000000000000101
int A = B << 3; // binary: 0000000000101000
A == 40;

The “bitshift right” operator is “>>”. The value of operand “B” is shifted to the right by position “C”. The bits to the right of operand “B” are extended and lost.

A = B >> C;

int B = 40;      // binary: 0000000000101000
int A = B >> 3;  // binary: 0000000000000101
A == 5;

Complex operators

The increment operator increments the value of the variable by one “++”. This can be used in two different ways. In the first example, the variable “A” passes its value to the variable “B” and then increases its value by one. In the second example, the variable “A” increments its value and then passes its value to the variable “B”. So it doesn’t matter which side of the operand the operator is on.

int A = 3;
int B;
B = A++;
// A == 4
// B == 3
int A = 3;
int B;
B = ++A;
// A == 4
// B == 4

The decrement operator “- -” decreases the value of the variable by one. This can be used in two different ways. In the first example, the variable “A” passes its value to the variable “B” and then decreases its value by one. In the second example, the variable “A” decreases its value and then passes its value to the variable “B”. So it doesn’t matter which side of the operand the operator is on.

int A = 3;
int B;
B = A--;
// A == 2
// B == 3
int A = 3;
int B;
B = --A;
// A == 2
// B == 2

The compound addition operator “+ =” adds the value of the right operand to the value of the left operand and assigns the result to the left operand. Operation A + = B is equivalent to A = A + B.

int A = 3;
int B = 2;
A += B;
// A == 5

The compound subtraction operator “- =” subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand. Operation A = = B is equivalent to A = A -B.

int A = 3;
int B = 2;
A -= B;
// A == 1

The compound multiplication operator “* =” multiplies the value of the left operand by the value of the right operand and assigns the result to the left operand. Operation A * = B is equivalent to A = A * B.

int A = 3;
int B = 2;
A *= B;
// A == 6

The compound division operator “/ =” divides the value of the left operand by the value of the right operand and assigns the result to the left operand. Operation A = B is equivalent to A = A / B.

int A = 8;
int B = 2;
A /= B;
// A == 4

compound remainder operator “%” divides the value of the left operand by the value of the right operand and assigns the remainder of the division to the left operand. Operation% = B is equivalent to A = A% B.

int A = 8;
int B = 5;
A %= B;
// A == 3

The compound bitwise or operator “| =” is often used to “reset” certain bits of a variable. Operation A = B is equivalent to A = A | B.

int myByte = 0b10101010;
int myMask = 0b00000011;
myByte |= myMask; 

// myByte == 0b10101011

1 0 1 0 1 0 1 0
0 0 0 0 0 0 1 1
---------------
1 0 1 0 1 0 1 1

The compound bitwise and operator is used “& =” to set certain bits in the variable to 0. This is also called “deleting bits” or “restoring”. So the zeros in the mask set the bits of the variable in the same place to zero, while leaving the rest of the variable unchanged. Operation A = B is equivalent to A = A & B.

int myByte = 0b10101010;
int myMask = 0b11111100;
myByte &= myMask; 

// myByte == 0b10101000

1 0 1 0 1 0 1 0
1 1 1 1 1 1 0 0
---------------
1 0 1 0 1 0 0 0

The compound bitwise xor operator “^ =” is often used with a variable and a constant to switch (invert) certain bits of the variable. So some in the mask keep setting the zeros of the variable in the same place, leaving the rest of the variable unchanged. Operation A = B is equivalent to A = A B.

int myByte = 0b10101010;
int myMask = 0b00000011;
myByte ^= myMask; 

// myByte == 0b10101001

1 0 1 0 1 0 1 0
0 0 0 0 0 0 1 1
---------------
1 0 1 0 1 0 0 1

Perfect for practice:

The next section revolves around conditional execution.