Operators Arduino
Operators Arduino
1. Arithmetic Operators
i) + (addition) -
Syntax -
sum = operand1 + operand2;
Parameters -
sum: variable. Allowed data types: int, float, double, byte, short, long.
operand1: variable or constant. Allowed data types: int, float, double, byte,
short, long.
operand2: variable or constant. Allowed data types: int, float, double, byte,
short, long.
Example Code -
int a = 5;
int b = 10;
int c = 0;
c = a + b;
ii) = (assignment operator) -
The single equal sign = in the C++ programming language is called the assignment operator. It
has a different meaning than in algebra class where it indicated an equation or equality. The
assignment operator tells the microcontroller to evaluate whatever value or expression is on
the right side of the equal sign, and store it in the variable to the left of the equal sign.
Example Code -
int sensVal; // declare an integer variable named sensVal
sensVal = analogRead(0); // store the (digitized) input voltage at analog pin 0 in SensVal
iii) / (division) -
Division is one of the four primary arithmetic operations. The operator / (slash) operates on
two operands to produce the result.
Syntax -
result = numerator / denominator;
Parameters -
result: variable. Allowed data types: int, float, double, byte, short, long.
numerator: variable or constant. Allowed data types: int, float, double, byte, short, long.
denominator: non zero variable or constant. Allowed data types: int, float, double, byte,
short, long.
Example Code -
int a = 50;
int b = 10;
int c = 0;
c = a / b;
iv) * (multiplication) -
Multiplication is one of the four primary arithmetic operations. The operator *
(asterisk) operates on two operands to produce the product.
Syntax -
product = operand1 * operand2;
Parameters -
product: variable. Allowed data types: int, float, double, byte, short, long.
operand1: variable or constant. Allowed data types: int, float, double, byte,
short, long.
operand2: variable or constant. Allowed data types: int, float, double, byte,
short, long.
Example Code -
int a = 5;
int b = 10;
int c = 0;
c = a * b; // the variable 'c' gets a value of 50 after this statement is
executed
v) % (remainder) -
Description -
Remainder operation calculates the remainder when one integer is divided by
another. It is useful for keeping a variable within a particular range (e.g. the
size of an array). The % (percent) symbol is used to carry out remainder
operation.
Syntax -
remainder = dividend % divisor;
Parameters -
remainder: variable. Allowed data types: int, float, double.
dividend: variable or constant. Allowed data types: int.
divisor: non zero variable or constant. Allowed data types: int.
Example Code -
int x = 0;
x = 7 % 5; // x now contains 2
x = 9 % 5; // x now contains 4
x = 5 % 5; // x now contains 0
x = 4 % 5; // x now contains 4
x = -4 % 5; // x now contains -4
x = 4 % -5; // x now contains 4
int values[10];
int i = 0;
void setup() {}
void loop() {
values[i] = analogRead(0);
i = (i + 1) % 10; // remainder operator rolls over variable
}
vi) - (subtraction) -
Subtraction is one of the four primary arithmetic operations. The operator -
(minus) operates on two operands to produce the difference of the second from
the first.
Syntax-
difference = operand1 - operand2;
Parameters-
difference: variable. Allowed data types: int, float, double, byte, short, long.
operand1: variable or constant. Allowed data types: int, float, double, byte,
short, long.
operand2: variable or constant. Allowed data types: int, float, double, byte,
short, long.
Example Code -
int a = 5;
int b = 10;
int c = 0;
c = a - b; // the variable 'c' gets a value of -5 after this statement is executed
2. Bitwise Operators
1. << (bitshift left) -
The left shift operator << causes the bits of the left operand to be shifted left by
the number of positions specified by the right operand.
Syntax -
variable << number_of_bits;
Parameters -
variable: Allowed data types: byte, int, long.
number_of_bits: a number that is < = 32. Allowed data types: int.
Example Code -
int a = 5; // binary: 0000000000000101
int b = a << 3; // binary: 0000000000101000, or 40 in decimal
2. >> (bitshift right) -
The right shift operator >> causes the bits of the left operand to be shifted right
by the number of positions specified by the right operand.
Syntax
variable >> number_of_bits;
Parameters
variable: Allowed data types: any integer type (byte, short, int, long, unsigned
short…).
number_of_bits: a positive number smaller than the bit-width of variable.
Allowed data types: int.
Example Code
int a = 40; // binary: 0000000000101000
int b = a >> 3; // binary: 0000000000000101, decimal: 5
3. & (bitwise AND) -
The bitwise AND operator in C++ is a single ampersand &, used between two
other integer expressions. Bitwise AND operates on each bit position of the
surrounding expressions independently, according to this rule: if both input
bits are 1, the resulting output is 1, otherwise the output is 0.
Another way of expressing this is:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) - returned result
In Arduino, the type int is a 16-bit value, so using & between two int
expressions causes 16 simultaneous AND operations to occur.
Example Code
In a code fragment like:
0 1 operand1
-----
1 0 ~operand1
Example Code
The bitwise OR operator in C++ is the vertical bar symbol, |. Like the &
operator, | operates independently each bit in its two surrounding integer
expressions, but what it does is different (of course). The bitwise OR of two
bits is 1 if either or both of the input bits is 1, otherwise it is 0.
In other words:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 1 (operand1 | operand2) - returned result
Example Code
There is a somewhat unusual operator in C++ called bitwise EXCLUSIVE OR, also
known as bitwise XOR. (In English this is usually pronounced "eks-or".) The
bitwise XOR operator is written using the caret symbol ^. A bitwise XOR
operation results in a 1 only if the input bits are different, else it results in a 0.
Precisely,
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 0 (operand1 ^ operand2) - returned result
Example Code
if (x == y) { // tests if x is equal to y
// do something only if the comparison result is true
}
ii. > (greater than) -
Compares the variable on the left with the value or variable on the right of the
operator. Returns true when the operand on the left is greater (bigger) than the
operand on the right. Please note that you may compare variables of different
data types, but that could generate unpredictable results, it is therefore
recommended to compare variables of the same data type including the
signed/unsigned type.
Syntax
x > y; // is true if x is bigger than y and it is false if x is equal or smaller than y
Parameters
x: variable. Allowed data types: int, float, double, byte, short, long.
y: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
Compares the variable on the left with the value or variable on the right of the
operator. Returns true when the operand on the left is less (smaller) than the
operand on the right. Please note that you may compare variables of different
data types, but that could generate unpredictable results, it is therefore
recommended to compare variables of the same data type including the
signed/unsigned type.
Syntax
x < y; // is true if x is smaller than y and it is false if x is equal or bigger than y
Parameters
x: variable. Allowed data types: int, float, double, byte, short, long.
y: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
Logical NOT results in a true if the operand is false and vice versa.
Example Code
This operator can be used inside the condition of an if statement.
Example Code
This operator can be used inside the condition of an if statement.
i) += (compound addition) -
x = 2;
x += 4; // x now contains 6
ii) &= (compound bitwise AND) -
The compound bitwise AND operator &= is often used with a variable and a constant to force particular bits in a variable
to the LOW state (to 0). This is often referred to in programming guides as "clearing" or "resetting" bits.
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 0 0 1 (operand1 & operand2) - returned result
Syntax
x &= y; // equivalent to x = x & y;
Parameters
x: variable. Allowed data types: char, int, long.
y: variable or constant. Allowed data types: char, int, long.
Example Code
Bits that are "bitwise ANDed" with 0 are cleared to 0 so, if myByte is a byte variable,
Syntax
x /= y; // equivalent to the expression x = x / y;
Parameters
x: variable. Allowed data types: int, float, double, byte, short, long.
y: non zero variable or constant. Allowed data types: int, float, double, byte,
short, long.
Example Code
x = 2;
x /= 2; // x now contains 1
vi) *= (compound multiplication) -
Syntax
x *= y; // equivalent to the expression x = x * y;
Parameters
x: variable. Allowed data types: int, float, double, byte, short, long.
y: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
x = 2;
x *= 3; // x now contains 6
vii) %= (compound remainder) -
Syntax
x %= divisor; // equivalent to the expression x = x % divisor;
Parameters
x: variable. Allowed data types: int.
divisor: non zero variable or constant. Allowed data types: int.
Example Code
int x = 7;
x %= 5; // x now contains 2
viii) -= (compound subtraction) -
Syntax
x -= y; // equivalent to the expression x = x - y;
Parameters
x: variable. Allowed data types: int, float, double, byte, short, long.
y: variable or constant. Allowed data types: int, float, double, byte, short, long.
Example Code
x = 20;
x -= 2; // x now contains 18
ix) -- (decrement) -
Syntax
x--; // decrement x by one and returns the old value of x
--x; // decrement x by one and returns the new value of x
Parameters
x: variable. Allowed data types: int, long (possibly unsigned).
Returns
The original or newly decremented value of the variable.
Example Code
x = 2;
y = --x; // x now contains 1, y contains 1
y = x--; // x contains 0, but y still contains 1
x) ++ (increment) -
Syntax
x{plus}{plus}; // increment x by one and returns the old value of x
{plus}{plus}x; // increment x by one and returns the new value of x
Parameters
x: variable. Allowed data types: int, long (possibly unsigned).
Returns
The original or newly incremented value of the variable.
Example Code
x = 2;
y = ++x; // x now contains 3, y contains 3
y = x++; // x contains 4, but y still contains 3