0% found this document useful (0 votes)
0 views

Operators Arduino

Uploaded by

timtmemory.2025
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Operators Arduino

Uploaded by

timtmemory.2025
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

OPERATORS

1. Arithmetic Operators
i) + (addition) -

Addition is one of the four primary arithmetic operations. The operator +


(plus) operates on two operands to produce the sum.

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

/* update one value in an array each time through a loop */

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:

int a = 92; // in binary: 0000000001011100


int b = 101; // in binary: 0000000001100101
int c = a & b; // result: 0000000001000100, or 68 in decimal.
4. ~ (bitwise NOT) -
The bitwise NOT operator in C++ is the tilde character ~. Unlike & and |, the
bitwise NOT operator is applied to a single operand to its right. Bitwise NOT
changes each bit to its opposite: 0 becomes 1, and 1 becomes 0.
In other words:

0 1 operand1
-----
1 0 ~operand1
Example Code

int a = 103; // binary: 0000000001100111


int b = ~a; // binary: 1111111110011000 = -104
5. | (bitwise OR) -

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

int a = 92; // in binary: 0000000001011100


int b = 101; // in binary: 0000000001100101
int c = a | b; // result: 0000000001111101, or 125 in decimal.
6. ^ (bitwise XOR) -

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

int x = 12; // binary: 1100


int y = 10; // binary: 1010
int z = x ^ y; // binary: 0110, or decimal 6
3. Comparison Operators
i. == (equal to) -
Compares the variable on the left with the value or variable on the right of the
operator. Returns true when the two operands are equal. 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 equal to y and it is false if x is not equal to 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

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

if (x > y) { // tests if x is greater (bigger) than y


// do something only if the comparison result is true
}
iii) >= (greater than or equal to) -
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 or equal to
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 or equal to y and it is false if x is 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

if (x >= y) { // tests if x is greater (bigger) than or equal to y


// do something only if the comparison result is true
}
iv. < (less 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 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

if (x < y) { // tests if x is less (smaller) than y


// do something only if the comparison result is true
}
5. <= (less than or equal to) -
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 or
equal to 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 or equal to y and it is false if x is greater 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
if (x <= y) { // tests if x is less (smaller) than or equal to y
// do something only if the comparison result is true
}
6. != (not equal to) -
Compares the variable on the left with the value or variable on the right of the
operator. Returns true when the two operands are not equal. 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 false if x is equal to y and it is true if x is not equal to 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
if (x != y) { // tests if x is not equal to y
// do something only if the comparison result is true
}
4. Boolean Operators

i) && (logical AND) -


Logical AND results in true only if both operands are true.
Example Code
This operator can be used inside the condition of an if statement.
if (digitalRead(2) == HIGH && digitalRead(3) == HIGH) { // if BOTH the switches
read HIGH
// statements
}
ii) ! (logical NOT) -

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.

if (!x) { // if x is not true


// statements
}
iii) || (logical OR) -

Logical OR results in a true if either of the two operands is true.

Example Code
This operator can be used inside the condition of an if statement.

if (x > 0 || y > 0) { // if either x or y is greater than zero


// statements
}
5. Compound Operators

i) += (compound addition) -

This is a convenient shorthand to perform addition on a variable with another


constant or variable.
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 += 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.

A review of the Bitwise AND & operator:

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,

myByte & 0b00000000 = 0;


Bits that are "bitwise ANDed" with 1 are unchanged so,

myByte & 0b11111111 = myByte;


iii) |= (compound bitwise OR) -
The compound bitwise OR operator |= is often used with a variable and a
constant to "set" (set to 1) particular bits in a variable.
A review of the Bitwise OR | operator:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 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 ORed" with 0 are unchanged, so if myByte is a byte
variable,

myByte | 0b00000000 = myByte;


Bits that are "bitwise ORed" with 1 are set to 1 so:

myByte | 0b11111111 = 0b11111111;


iv) ^= (compound bitwise XOR) -
The compound bitwise XOR operator ^= is often used with a variable and a
constant to toggle (invert) particular bits in a variable.
A review of the Bitwise XOR ^ operator:
0 0 1 1 operand1
0 1 0 1 operand2
----------
0 1 1 0 (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 XORed" with 0 are left unchanged. So if myByte is a byte
variable,
myByte ^ 0b00000000 = myByte;
Bits that are "bitwise XORed" with 1 are toggled so:

myByte ^ 0b11111111 = ~myByte;


v) /= (compound division) -

This is a convenient shorthand to perform division of a variable with another


constant or 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) -

This is a convenient shorthand to perform multiplication of a variable with


another constant or variable.

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) -

This is a convenient shorthand to calculate the remainder when one integer is


divided by another and assign it back to the variable the calculation was done
on.

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) -

This is a convenient shorthand to perform subtraction of a constant or a variable


from a variable.

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) -

Decrements the value of a variable by 1.

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) -

Increments the value of a variable by 1.

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

You might also like