C++ operators
An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C++ has a rich set of built-in operators and provides the following types of operators:
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
- ##Assignment operators
- Miscellaneous operators
Examples
See the examples below to learn about the arithmetic operators available in C++.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
#include <iostream> using namespace std; main() { int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - c 的值是 " << c << endl ; c = a - b; cout << "Line 2 - c 的值是 " << c << endl ; c = a * b; cout << "Line 3 - c 的值是 " << c << endl ; c = a / b; cout << "Line 4 - c 的值是 " << c << endl ; c = a % b; cout << "Line 5 - c 的值是 " << c << endl ; c = a++; cout << "Line 6 - c 的值是 " << c << endl ; c = a--; cout << "Line 7 - c 的值是 " << c << endl ; return 0; }
When the above code is compiled and executed, it produces the following results:
Line 1 - c 的值是 31 Line 2 - c 的值是 11 Line 3 - c 的值是 210 Line 4 - c 的值是 2 Line 5 - c 的值是 1 Line 6 - c 的值是 21 Line 7 - c 的值是 22
Relational operators
The following table shows the relational operators supported by C++.
Assume that the value of variable A is 10 and the value of variable B is 20, then:
Divide the numerator by the denominator | B / A will get 2 | |
Operator | Description | Example |
---|---|---|
== | Check whether the values of the two operands are equal. If they are equal, the condition is true. | (A == B) is not true. |
!= | Checks whether the values of the two operands are equal. If they are not equal, the condition is true. | (A != B) is true. |
> | Checks whether the value of the left operand is greater than the value of the right operand, if so, the condition is true. | (A > B) is not true. |
< | Checks whether the value of the left operand is less than the value of the right operand, if so the condition is true. | (A < B) is true. |
>= | Checks whether the value of the left operand is greater than or equal to the value of the right operand, if so the condition is true. | (A >= B) is not true. |
<= | Checks whether the value of the left operand is less than or equal to the value of the right operand, if so the condition is true. | (A <= B) is true. |
Examples
See the examples below to learn about the relational operators available in C++.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
#include <iostream> using namespace std; main() { int a = 21; int b = 10; int c ; if( a == b ) { cout << "Line 1 - a 等于 b" << endl ; } else { cout << "Line 1 - a 不等于 b" << endl ; } if ( a < b ) { cout << "Line 2 - a 小于 b" << endl ; } else { cout << "Line 2 - a 不小于 b" << endl ; } if ( a > b ) { cout << "Line 3 - a 大于 b" << endl ; } else { cout << "Line 3 - a 不大于 b" << endl ; } /* 改变 a 和 b 的值 */ a = 5; b = 20; if ( a <= b ) { cout << "Line 4 - a 小于或等于 b" << endl ; } if ( b >= a ) { cout << "Line 5 - b 大于或等于 a" << endl ; } return 0; }
When the above code is compiled and executed, it produces the following results:
Line 1 - a 不等于 b Line 2 - a 不小于 b Line 3 - a 大于 b Line 4 - a 小于或等于 b Line 5 - b 大于或等于 a
Logical operators
The following table shows the relational logical operators supported by C++ .
Assume that the value of variable A is 1 and the value of variable B is 0, then:
Operator | Description | Example |
---|---|---|
&& | is called the logical AND operator. The condition is true if both operands are non-zero. | (A && B) is false. |
|| | is called the logical OR operator. The condition is true if either of the two operands is non-zero. | (A || B) is true. |
! | is called the logical NOT operator. Used to reverse the logical state of the operand. The logical NOT operator will make the condition false if it is true. | !(A && B) is true. |
Examples
See the examples below to learn about the logical operators available in C++.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
#include <iostream> using namespace std; main() { int a = 5; int b = 20; int c ; if ( a && b ) { cout << "Line 1 - 条件为真"<< endl ; } if ( a || b ) { cout << "Line 2 - 条件为真"<< endl ; } /* 改变 a 和 b 的值 */ a = 0; b = 10; if ( a && b ) { cout << "Line 3 - 条件为真"<< endl ; } else { cout << "Line 4 - 条件不为真"<< endl ; } if ( !(a && b) ) { cout << "Line 5 - 条件为真"<< endl ; } return 0; }
When the above code is compiled and executed, it produces the following results:
Line 1 - 条件为真 Line 2 - 条件为真 Line 4 - 条件不为真 Line 5 - 条件为真
Bit operators
Bit operators act on bits and are executed bit by bit operate. 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 |
Examples
See the examples below to learn about the bitwise operators available in C++.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
#include <iostream> using namespace std; main() { unsigned int a = 60; // 60 = 0011 1100 unsigned int b = 13; // 13 = 0000 1101 int c = 0; c = a & b; // 12 = 0000 1100 cout << "Line 1 - c 的值是 " << c << endl ; c = a | b; // 61 = 0011 1101 cout << "Line 2 - c 的值是 " << c << endl ; c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - c 的值是 " << c << endl ; c = ~a; // -61 = 1100 0011 cout << "Line 4 - c 的值是 " << c << endl ; c = a << 2; // 240 = 1111 0000 cout << "Line 5 - c 的值是 " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - c 的值是 " << c << endl ; return 0; }
When the above code is compiled and executed, it produces the following results:
Line 1 - c 的值是 12 Line 2 - c 的值是 61 Line 3 - c 的值是 49 Line 4 - c 的值是 -61 Line 5 - c 的值是 240 Line 6 - c 的值是 15
Assignment Operators
The following table lists the assignment operators supported by C++ :
Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand. | A >> 2 will get 15, which is 0000 1111 |
Examples
See the examples below to learn about the assignment operators available in C++.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
#include <iostream> using namespace std; main() { int a = 21; int c ; c = a; cout << "Line 1 - = 运算符实例,c 的值 = : " <<c<< endl ; c += a; cout << "Line 2 - += 运算符实例,c 的值 = : " <<c<< endl ; c -= a; cout << "Line 3 - -= 运算符实例,c 的值 = : " <<c<< endl ; c *= a; cout << "Line 4 - *= 运算符实例,c 的值 = : " <<c<< endl ; c /= a; cout << "Line 5 - /= 运算符实例,c 的值 = : " <<c<< endl ; c = 200; c %= a; cout << "Line 6 - %= 运算符实例,c 的值 = : " <<c<< endl ; c <<= 2; cout << "Line 7 - <<= 运算符实例,c 的值 = : " <<c<< endl ; c >>= 2; cout << "Line 8 - >>= 运算符实例,c 的值 = : " <<c<< endl ; c &= 2; cout << "Line 9 - &= 运算符实例,c 的值 = : " <<c<< endl ; c ^= 2; cout << "Line 10 - ^= 运算符实例,c 的值 = : " <<c<< endl ; c |= 2; cout << "Line 11 - |= 运算符实例,c 的值 = : " <<c<< endl ; return 0; }
When the above code is compiled and executed, it produces the following results:
Line 1 - = 运算符实例,c 的值 = 21 Line 2 - += 运算符实例,c 的值 = 42 Line 3 - -= 运算符实例,c 的值 = 21 Line 4 - *= 运算符实例,c 的值 = 441 Line 5 - /= 运算符实例,c 的值 = 21 Line 6 - %= 运算符实例,c 的值 = 11 Line 7 - <<= 运算符实例,c 的值 = 44 Line 8 - >>= 运算符实例,c 的值 = 11 Line 9 - &= 运算符实例,c 的值 = 2 Line 10 - ^= 运算符实例,c 的值 = 0 Line 11 - |= 运算符实例,c 的值 = 2
Miscellaneous operators
The following table lists some other important ones supported by C++ operator.
Operator | Description | Instance |
---|---|---|
= | Simple assignment operator, assigns the value of the right operand to the left operand | C = A + B will assign the value of A + B to C |
+= | Addition and assignment operator, assigns the result of adding the right operand to the left operand to the left operand | C += A is equivalent to C = C + A |
-= | Subtraction and assignment operator, assigns the result of subtracting the right operand from the left operand to the left operand | C -= A is equivalent to C = C - A |
*= | The multiplication and assignment operator assigns the result of multiplying the right operand by the left operand to the left operand | C *= A is equivalent to C = C * A |
Right shift and assignment operator | C >>= 2 is equivalent to C = C >> 2 | |
Press Bitwise AND and assignment operator | C &= 2 is equivalent to C = C & 2 | |
Bitwise XOR and assignment operator The symbol | C ^= 2 is equivalent to C = C ^ 2 | |
Operator | Description |
---|---|
sizeof | sizeof operator returns the size of the variable . For example, sizeof(a) returns 4, where a is an integer. |
Condition ? X : Y | Conditional operator. The value is X if Condition is true ? : otherwise the value is Y. |
, | The comma operator performs a series of operations sequentially. The value of the entire comma expression is the value of the last expression in the comma-separated list. |
. (dot) and -> (arrow) | Member operators are used to refer to members of classes, structures, and unions. |
Cast | The cast operator converts one data type to another data type. For example, int(2.2000) returns 2. |
& | Pointer operator & returns the address of the variable. For example &a; will give the actual address of the variable. |
* | Pointer operator * points to a variable. For example, *var; will point to the variable var. |
Operator precedence in C++
The precedence of an operator determines the combination of terms in an expression. This affects how an expression is evaluated. Some operators have higher precedence than others, for example, multiplication and division operators have higher precedence than addition and subtraction operators.
For example x = 7 + 3 * 2, here, x is assigned the value 13 instead of 20, because the operator * has a higher priority than +, so the multiplication 3*2 is calculated first, and then Plus 7 more.
The following table lists each operator according to operator priority from high to low. Operators with higher priority appear at the top of the table, and operators with lower priority appear at the bottom of the table. under. In an expression, operators with higher precedence are evaluated first.
Category | Operator | Associativity |
---|---|---|
Suffix | () [] -> . ++ - - | From left to right |
One yuan | + - ! ~ ++ - - (type)* & sizeof | From right to left |
Multiplication and division | * / % | From left to right |
Addition and subtraction | + - | 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 |
Bit XOR XOR | ^ | From left to right |
Bit OR | | | From left to right |
Logic AND | && | from left to right |
logical OR | || | from left To the right |
Condition | ?: | From right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | From right to left |
comma | , | From left to right |
Example
Please see the following example to learn about C++ The precedence of the operator.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
Compare the difference between parentheses and without parentheses, which will produce different results. Because (), /, *, and + have different precedence, the higher precedence operator will be evaluated first.
#include <iostream> using namespace std; main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 cout << "(a + b) * c / d 的值是 " << e << endl ; e = ((a + b) * c) / d; // (30 * 15 ) / 5 cout << "((a + b) * c) / d 的值是 " << e << endl ; e = (a + b) * (c / d); // (30) * (15/5) cout << "(a + b) * (c / d) 的值是 " << e << endl ; e = a + (b * c) / d; // 20 + (150/5) cout << "a + (b * c) / d 的值是 " << e << endl ; return 0; }
When the above code is compiled and executed, it produces the following results:
(a + b) * c / d 的值是 90 ((a + b) * c) / d 的值是 90 (a + b) * (c / d) 的值是 90 a + (b * c) / d 的值是 50