techmore.in

Arduino - Operators

In Arduino programming, operators are symbols that perform operations on variables and values. They are similar to operators in other programming languages like C and C++. Arduino supports a wide range of operators, including arithmetic, comparison, logical, bitwise, and assignment operators. Here's an overview of the commonly used operators in Arduino:

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.

  • Addition (+): Adds two operands.

    cpp
    int result = 10 + 5; // result is 15
  • Subtraction (-): Subtracts right operand from left operand.

    cpp
    int result = 10 - 5; // result is 5
  • Multiplication (*): Multiplies two operands.

    cpp
    int result = 10 * 5; // result is 50
  • Division (/): Divides left operand by right operand.

    cpp
    int result = 10 / 5; // result is 2
  • Modulus (%): Computes remainder of division of left operand by right operand.

    cpp
    int result = 10 % 3; // result is 1 (remainder of 10 divided by 3)

Comparison Operators

Comparison operators are used to compare two values. They return a boolean value (true or false).

  • Equal to (==): Checks if the values of two operands are equal.

    cpp
    bool isEqual = (10 == 5); // isEqual is false
  • Not equal to (!=): Checks if the values of two operands are not equal.

    cpp
    bool isNotEqual = (10 != 5); // isNotEqual is true
  • Greater than (>): Checks if left operand is greater than right operand.

    cpp
    bool isGreater = (10 > 5); // isGreater is true
  • Less than (<): Checks if left operand is less than right operand.

    cpp
    bool isLess = (10 < 5); // isLess is false
  • Greater than or equal to (>=): Checks if left operand is greater than or equal to right operand.

    cpp
    bool isGreaterOrEqual = (10 >= 5); // isGreaterOrEqual is true
  • Less than or equal to (<=): Checks if left operand is less than or equal to right operand.

    cpp
    bool isLessOrEqual = (10 <= 5); // isLessOrEqual is false

Logical Operators

Logical operators are used to combine multiple conditions.

  • Logical AND (&&): Returns true if both operands are true.

    cpp
    bool result = (true && false); // result is false
  • Logical OR (||): Returns true if either operand is true.

    cpp
    bool result = (true || false); // result is true
  • Logical NOT (!): Reverses the logical state of its operand.

    cpp
    bool result = !true; // result is false

Bitwise Operators

Bitwise operators perform operations on bits of integers.

  • Bitwise AND (&): Performs bitwise AND on two integers.

    cpp
    int result = 5 & 3; // result is 1 (binary: 101 & 011 = 001)
  • Bitwise OR (|): Performs bitwise OR on two integers.

    cpp
    int result = 5 | 3; // result is 7 (binary: 101 | 011 = 111)
  • Bitwise XOR (^): Performs bitwise XOR on two integers.

    cpp
    int result = 5 ^ 3; // result is 6 (binary: 101 ^ 011 = 110)
  • Bitwise NOT (~): Inverts all bits of an integer.

    cpp
    int result = ~5; // result is -6 (binary: ~00000101 = 11111010)
  • Left Shift (<<): Shifts bits to the left by a specified number of positions.

    cpp
    int result = 5 << 2; // result is 20 (binary: 00000101 << 2 = 00010100)
  • Right Shift (>>): Shifts bits to the right by a specified number of positions.

    cpp
    int result = 5 >> 1; // result is 2 (binary: 00000101 >> 1 = 00000010)

Assignment Operators

Assignment operators are used to assign values to variables.

  • Assignment (=): Assigns right operand's value to left operand.

    cpp
    int a = 10;
  • Addition assignment (+=): Adds right operand to left operand and assigns the result to left operand.

    cpp
    int a = 10; a += 5; // equivalent to a = a + 5; (a is now 15)
  • Subtraction assignment (-=): Subtracts right operand from left operand and assigns the result to left operand.

    cpp
    int a = 10; a -= 3; // equivalent to a = a - 3; (a is now 7)
  • Multiplication assignment (*=): Multiplies right operand with left operand and assigns the result to left operand.

    cpp
    int a = 10; a *= 2; // equivalent to a = a * 2; (a is now 20)
  • Division assignment (/=): Divides left operand by right operand and assigns the result to left operand.

    cpp
    int a = 10; a /= 3; // equivalent to a = a / 3; (a is now 3)
  • Modulus assignment (%=): Computes remainder after division of left operand by right operand and assigns the result to left operand.

    cpp
    int a = 10; a %= 3; // equivalent to a = a % 3; (a is now 1)

Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1.

  • Increment (++): Increases the value of the operand by 1.

    cpp
    int a = 5; a++; // equivalent to a = a + 1; (a is now 6)
  • Decrement (--): Decreases the value of the operand by 1.

    cpp
    int a = 5; a--; // equivalent to a = a - 1; (a is now 4)

Conditional Operator (Ternary Operator)

The conditional operator (?:) is used for conditional expressions and is shorthand for if-else statements.

cpp
int a = 5; int b = 10; int max = (a > b) ? a : b; // max is assigned the larger of a and b

Operator Precedence

Operators in Arduino, as in C/C++, follow operator precedence rules. Operators with higher precedence are evaluated first. Parentheses () can be used to override precedence.