Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C++ has a wealth of built-in operators. This article will introduce arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators and other operators one by one.
Arithmetic Operator
The following table shows all arithmetic operators supported by C++.
Assuming the value of variable A is 10 and the value of variable B is 20, then:
Instance
Please see the example below to understand all 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 will produce the following results:
Line 1-the value of c is 31
Line 2-the value of c is 11
Line 3-the value of c is 210
Line 4-the value of c is 2
Line 5-the value of c is 1
Line 6-the value of c is 21
Line 7-the value of c is 22
Relational operator
The following table shows all the relational operators supported by C++.
Assuming the value of variable A is 10 and the value of variable B is 20, then:
Instance
Please see the example below to understand all 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 ;
}
/* Change the value of a and b*/
a = 5;
b = 20;
if ( a <= b )
{
cout << "Line 4-a is less than or equal to b" << endl;
}
if ( b >= a )
{
cout << "Line 5-b is greater than or equal to b" << endl;
}
return 0;
}
When the above code is compiled and executed, it will produce the following results:
Line 1-a is not equal to b
Line 2-a is not less than b
Line 3-a is greater than b
Line 4-a is less than or equal to b
Line 5-b is greater than or equal to b
Logical Operators
The following table shows all the relational logical operators supported by C++.
Assuming that the value of variable A is 1, and the value of variable B is 0, then:
Instance
Please see the example below to understand all 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-condition is true"<< endl;
}
if ( a || b )
{
cout << "Line 2-condition is true"<< endl;
}
/* Change the value of a and b*/
a = 0;
b = 10;
if ( a && b )
{
cout << "Line 3-condition is true"<< endl;
}
else
{
cout << "Line 4-condition is not true"<< endl;
}
if ( !(a && b) )
{
cout << "Line 5-condition is true"<< endl;
}
return 0;
}
When the above code is compiled and executed, it will produce the following results:
Line 1-condition is true
Line 2-condition is true
Line 3-condition is not true
Line 4-condition is true
Bit operator
Bit operators act on bits and perform operations bit by bit. The truth table of &, | and ^ is as follows:
Suppose if A = 60 and B = 13, they are now expressed in binary format, and 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 bitwise operators supported by C++. Assuming the value of variable A is 60 and the value of variable B is 13, then:
Instance
Please see the example below to understand all 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 will produce the following results:
Line 1-the value of c is 12
Line 2-the value of c is 61
Line 3-the value of c is 49
Line 4-the value of c is -61
Line 5-the value of c is 240
Line 6-the value of c is 15
Assignment operator
The following table lists the assignment operators supported by C++:
Instance
Please see the example below to understand all 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-= operator instance, the value of c =: "<<c<< endl;
c += a;
cout << "Line 2-+= operator instance, the value of c =: "<<c<< endl;
c -= a;
cout << "Line 3--= operator instance, the value of c =: "<<c<< endl;
c *= a;
cout << "Line 4-*= operator instance, the value of c =: "<<c<< endl;
c /= a;
cout << "Line 5-/= operator instance, the value of c =: "<<c<< endl;
c = 200;
c %= a;
cout << "Line 6-%= operator instance, the value of c =: "<<c<< endl;
c <<= 2;
cout << "Line 7-<<= operator instance, the value of c =: "<<c<< endl;
c >>= 2;
cout << "Line 8->>= operator instance, the value of c =: "<<c<< endl;
c &= 2;
cout << "Line 9-&= operator instance, the value of c =: "<<c<< endl;
c ^= 2;
cout << "Line 10-^= operator instance, the value of c =: "<<c<< endl;
c |= 2;
cout << "Line 11-|= operator instance, the value of c =: "<<c<< endl;
return 0;
}
When the above code is compiled and executed, it will produce the following results:
Line 1-= operator instance, the value of c = 21
Line 2-+= operator instance, the value of c = 42
Line 3--= operator instance, the value of c = 21
Line 4-*= operator instance, the value of c = 441
Line 5-/= operator instance, the value of c = 21
Line 6-%= operator instance, the value of c = 11
Line 7-<<= operator instance, the value of c = 44
Line 8->>= operator instance, the value of c = 11
Line 9-&= operator instance, the value of c = 2
Line 10-^= operator instance, the value of c = 0
Line 11-|= operator instance, the value of c = 2
Miscellaneous operators
The following table lists some other important operators supported by C++.
Operator precedence in C++
The precedence of operators determines the combination of terms in the expression. This will affect how an expression is calculated. 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, where x is assigned the value 13 instead of 20. Because the operator has a higher priority than +, the multiplication 3*2 is calculated first, and then 7 is added.
The following table lists the operators in descending order of operator precedence. Operators with higher precedence appear above the table, and operators with lower precedence appear below the table. In expressions, operators with higher precedence will be evaluated first.
Instance
Please see the example below to understand the precedence of operators in C++.
Copy and paste the following C++ program into the test.cpp file, compile and run the program.
Compare the difference between parentheses and no parentheses. This will produce different results. Because (), /, * and + have different precedence, operators with higher precedence 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 will produce the following results:
(a + b) * c / d is 90
((a + b) * c) / d is 90
(a + b) * (c / d) is 90
the value of a + (b * c) / d is 50
This article mainly introduces the operators in C++ and the summary of operator precedence, I hope it will be helpful to everyone!
If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!
Programming learning books:
Programming learning video: