In this article we will learn about operator in c++ and it’s precedence.

so, let’s starts with the operator

## What is Operator in C++ programming?

An **operator** in a C++ **programming** language is a symbol that tells the compiler or interpreter to perform the specific mathematical, relational, or logical operations and produce the final result.

## Types of Operator

- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators

## Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

In the example below, we will use the arithmetic `+`

**operator** to add together two values:

int x = 100 + 50;

Operator | Name | Description | Example |
---|---|---|---|

+ | Addition | Adds together two values | x + y |

– | Subtraction | Subtracts one value from another | x – y |

* | Multiplication | Multiplies two values | x * y |

/ | Division | Divides one value by another | x / y |

% | Modulus | Returns the division remainder | x % y |

++ | Increment | Increases the value of a variable by 1 | ++x |

— | Decrement | Decreases the value of a variable by 1 | –x |

## Assignment operators

Assignment operators are used to assign values to variables.

In the example below, we use the **assignment** operator (`=`

) to assign the value **10** to a variable called **x**:

int x = 100 + 50;

Although the `+`

operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:

**A list of all assignment operators:**

Operator | Example | Same As |
---|---|---|

= | x = 5 | x = 5 |

+= | x += 3 | x = x + 3 |

-= | x -= 3 | x = x – 3 |

*= | x *= 3 | x = x * 3 |

/= | x /= 3 | x = x / 3 |

%= | x %= 3 | x = x % 3 |

&= | x &= 3 | x = x & 3 |

|= | x |= 3 | x = x | 3 |

^= | x ^= 3 | x = x ^ 3 |

>>= | x >>= 3 | x = x >> 3 |

<<= | x <<= 3 | x = x << 3 |

## Comparison Operator

Comparison operators are used to compare two values.

**Note:** The return value of a comparison is either true (`1`

) or false (`0`

).

In the following example, we use the **greater than** operator (`>`

) to find out if 5 is greater than 3:

int x = 5; int y = 3; cout << (x > y); // returns 1 (true) because 5 is greater than 3

A list of all comparison operators:

Operator | Name | Example |
---|---|---|

== | Equal to | x == y |

!= | Not equal | x != y |

> | Greater than | x > y |

< | Less than | x < y |

>= | Greater than or equal to | x >= y |

<= | Less than or equal to | x <= y |

## Logical Operator

Logical operators are used to determining the logic between variables or values:

Below is the example of logical operator.

#include <iostream> using namespace std; int main() { int x = 5; int y = 3; cout << (x > 3 && x < 10); // returns true (1) because 5 is greater than 3 AND 5 is less than 10 return 0; }

Operator | Name | Description | Example |
---|---|---|---|

&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |

|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |

! | Logical not | Reverse the result, returns false if the result is true | !(x < 5 && x < 10) |

## Bitwise Operator

In C++, bitwise operators perform operations on integer data at the individual bit-level. These operations include testing, setting, or shifting the actual bits.

Here is a list of 6 bitwise operators included in C++.

Operator | Description |
---|---|

`&` | Bitwise AND Operator |

`|` | Bitwise OR Operator |

`^` | Bitwise XOR Operator |

`~` | Bitwise Complement Operator |

`<<` | Bitwise Shift Left Operator |

`>>` | Bitwise Shift Right Operator |

Below is the example of bitwise And operator

#include <iostream> using namespace std; int main() { // declare variables int a = 12, b = 25; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a & b = " << (a & b) << endl; return 0; }

## What is operator precedence in c++?

Operator **precedence** determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher **precedence** than others; for example, the multiplication operator has higher **precedence** than the addition operator.

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category | Operator | Associativity |
---|---|---|

Postfix | () [] -> . ++ – – | Left to right |

Unary | + – ! ~ ++ – – (type)* & sizeof | Right to left |

Multiplicative | * / % | Left to right |

Additive | + – | Left to right |

Shift | << >> | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Bitwise AND | & | Left to right |

Bitwise XOR | ^ | Left to right |

Bitwise OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |

Comma | , | Left to right |