# C++ Operators

The operator is a symbol that tells the compiler to perform specific mathematical or logical operations. C++ has built-in rich operators and provides the following types of operators:

- Arithmetic operators
- Relational operator
- Logical operator
- bit operator
- Assignment operator
- Miscellaneous operator

This chapter introduces arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators one by one.

## Arithmetic Operators

The following table shows the arithmetic operators supported by C++.

Assuming the value of variable A is 10 and the value of variable B is 20, then:

Operators | Description | Instances |
---|---|---|

+ | Adding two operands | A + B will get 30 |

- | Subtract the second operand from the first operand | A - B will get -10 |

* | Multiply two operands | A * B will get 200 |

/ | Molecular division by denominator | B / A will result in 2 |

% | modulus operator, remainder after divisibility | B % A will get 0 |

++ | Auto increment operator , the integer value is increased by 1 | A++ will get 11 |

-- | Decremental operator , the integer value is reduced by 1 | A-- will get 9 |

### example

See the example below for the arithmetic operators available in C++.

Copy and paste the following C++ program into the test.cpp file to compile and run the program.

When the above code is compiled and executed, it produces the following result:

Line 1 - c The value is 31 Line 2 - c The value is 11 Line 3 - c The value is 210 Line 4 - c The value is 2 Line 5 - c The value is 1 Line 6 - c The value is 10 Line 7 - c The value is 10

## relational operator

The following table shows the relational operators supported by C++.

Assuming the value of variable A is 10 and the value of variable B is 20, then:

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

== | Check the two operands The value is is equal, if the conditions are equal, the condition is true. | (A == B) Not true. |

!= | Check the two operands The value is is equal, if not equal, the condition is true. | (A != B) Be true. |

> | Check left operand The value is No is greater than the value of the right operand, and if so, the condition is true. | (A > B) Not true. |

< | Check left operand The value is No is less than the value of the right operand, and if so, the condition is true. | (A < B) Be true. |

>= | Check left operand The value is No is greater than or equal to the value of the right operand, and if so, the condition is true. | (A >= B) Not true. |

<= | Check left operand The value is No is less than or equal to the value of the right operand, and if so, the condition is true. | (A <= B)Be true. |

### example

See the example below for the relational operators available in C++.

Copy and paste the following C++ program into the test.cpp file to compile and run the program.

When the above code is compiled and executed, it produces the following result:

Line 1 - a Do not equal b Line 2 - a Not Less than b Line 3 - a more than the b Line 4 - a less than or equal to b Line 5 - b more than The or equal a

## logical operators

The following table shows the relational logical operators supported by C++.

Assuming the value of variable A is 1, the value of variable B is 0, then:

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

&& | is called a logical AND operator. If both operands are non-zero, the condition Be true. | (A && B) False。 |

|| | Called a logical OR operator. If any of the two operands is non-zero, the condition Be true. | (A || B) Be true. |

! | is called a logical non-operator. The logical state used to reverse the operands. A logical non-operator will make it false if the condition is true. | !(A && B) Be true. |

### example

See the example below for the logical operators available in C++.

Copy and paste the following C++ program into the test.cpp file to compile and run the program.

When the above code is compiled and executed, it produces the following result:

Line 1 - Condition is true< /span> Line 2 - Condition is true Line 4 - conditions Not true Line 5 - Condition is true

## bitwise operator

bit operator acts on bits and performs operations bit by bitThe truth table for and ^ is as follows:

。&、 |The truth table for and ^ is as follows:p | q | p & q | p | q | p ^ q |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

Suppose if A = 60 and B = 13, now in binary format, they are as follows:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The following table shows the bit operators supported by C++. Suppose the value of variable A is 60 and the value of variable B is 13, then:

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

& | If it exists in both operands, the binary AND operator copies one bit into the result. | (A & B) Will get 12，That is 0000 1100 |

| | If it exists in any operand, the binary OR operator copies one bit into the result。 | (A | B) Will get 61，That is 0011 1101 |

^ | If it exists in one of the operands but does not exist in both operands, the binary XOR operator copies one bit into the result. | (A ^ B) Will get 49，That is 0011 0001 |

~ | The two's complement operator is the One dollar operator with a "flip" bit effect, that is, 0 becomes 1, and 1 becomes 0. | (~A ) Will get -61，That is 1100 0011，One The complement form of a signed binary number. |

<< | Binary left shift operator. The value of the left operand moves to the left by the number of digits specified by the right operand. | A << 2 Will get 240，That is 1111 0000 |

>> | Binary right shift operator. The value of the left operand moves to the right by the number of digits specified by the right operand. | A >> 2 Will get 15，That is 0000 1111 |

### Instance

See the example below for the bitwise operators available in C++.

Copy and paste the following C++ program into the test.cpp file to compile and run the program.

## example

When the above code is compiled and executed, it produces the following result:

Line 1 - c The value is 12 Line 2 - c The value is 61 Line 3 - c The value is 49 Line 4 - c The value is -61 Line 5 - c The value is 240 Line 6 - c The value is 15

## Assignment Operator

The following table lists the assignment operators supported by C++:

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

= | Simple assignment operator，Assign the value of the right operand to the left operand | C = A + B will assign the value of A + B toC |

+= | Add and assign operator，Assign the result of the right operand plus the left operand to the left operand | C += A Equivalent C = C + A |

-= | Decrement and assignment operator，The result of subtracting the left operand from the left operand is assigned to the left operand | C -= A Equivalent C = C - A |

*= | Multiply and assign operator，The result of multiplying the right operand by the left operand is assigned to the left operand | C *= A Equivalent C = C * A |

/= | Assignment operator，Assign the result of dividing the left operand by the right operand to the left operand | C /= A Equivalent C = C / A |

%= | Modulo and assignment operator，Find the modulus assignment of two operands to the left operand | C %= A Equivalent C = C % A |

<<= | Left shift and assignment operator | C <<= 2 Equivalent to C = C << 2 |

>>= | Right shift and assignment operator | C >>= 2 Equivalent to C = C >> 2 |

&= | Bitwise and assignment operator | C &= 2 Equivalent to C = C & 2 |

^= | Bitwise XOR and assignment operator | C ^= 2 Equivalent to C = C ^ 2 |

|= | Bitwise or assignment operator | C |= 2 Equivalent to C = C | 2 |

### example

See the example below for the assignment operators available in C++.

Copy and paste the following C++ program into the test.cpp file to compile and run the program.

When the above code is compiled and executed, it produces the following result:

Line 1 - = Operator instance，c Value = 21 Line 2 - += Operator instance，c Value = 42 Line 3 - -= Operator instance，c Value = 21 Line 4 - *= Operator instance，c Value = 441 Line 5 - /= Operator instance，c Value = 21 Line 6 - %= Operator instance，c Value = 11 Line 7 - <<= Operator instance，c Value = 44 Line 8 - >>= Operator instance，c Value = 11 Line 9 - &= Operator instance，c Value = 2 Line 10 - ^= Operator instance，c Value = 0 Line 11 - |= Operator instance，c Value = 2

## Miscellaneous operator

The following table lists some of the other important operators supported by C++.

TheOperators | Description |
---|---|

sizeof | sizeof operator returns the size of the variable. For example, sizeof(a) will return 4, where a is an integer. |

Condition ? X : Y | conditional operator. If Condition is true ? then the value is X : otherwise the value is Y. |

, | comma operator performs a series of operations in sequence. The entire comma expression The value is the last expression Value in a comma-separated list. |

.(point) and ->(arrow) | Member operators are used to refer to members of classes, structures, and unions. |

Cast | Casting operator converts one data type to another. For example, int(2.2000) will return 2. |

& | Pointer operator & Return the address of the variable。E.g &a; Will give the actual address of the variable。 |

* | Pointer operator * Point to a variable.E.g，*var; Will point to the variable var。 |

## C++ Operator precedence in

The precedence of theoperator determines the combination of items in the expression. This affects how an expression is calculated. Some operators have higher precedence than other operators. For example, the Multiplication and division operator has a higher priority than the addition and subtraction operators.

For example x = 7 + 3 * 2, where x is assigned a value of 13, not 20, because the operator * has a higher priority than +, so first calculate the multiplication 3*2, then add 7.

The following table lists the operators by the operator priority from high to low. Operators with higher precedence appear above the table, and operators with lower precedence appear below the table. In expressions, higher priority operators are evaluated first.

category | Operator | Combination |
---|---|---|

suffix | () [] -> . ++ - - | From left to right |

One dollar | + - ! ~ ++ - - (type)* & sizeof | From right to left |

Multiplication and division | * / % | From left to right |

Add and subtract | + - | From left to right |

Shift | << >> | From left to right |

relationship | < <= > >= | From left to right |

equal | == != | From left to right |

Bit and AND | & | From left to right |

XOR XOR | ^ | From left to right |

Bit or OR | | | From left to right |

Logic and AND | && | From left to right |

Logical or OR | || | From left to right |

condition | ?: | From right to left |

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

comma | , | From left to right |

### Instance

See the example below for the precedence of operators in C++.

Copy and paste the following C++ program into the test.cpp file to compile and run the program.

Comparing the difference between parentheses and no parentheses, this will produce different results. Because (), /, *, and + have different priorities, high-priority operators are prioritized.

## example

When the above code is compiled and executed, it produces the following result:

(a + b) * c / d The value is 90 ((a + b) * c) / d The value is 90 (a + b) * (c / d) The value is 90 a + (b * c) / d The value is 50