WEB DEVELOPER SITE
PYTHONPHP7JAVA
 

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:

OperatorsDescriptionInstances
+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.

#include <iostream> using namespace std; int main() { int a = 21; int b = 10; int c; c = a + b; cout << "Line 1 - c The value is " << c << endl ; c = a - b; cout << "Line 2 - c The value is " << c << endl ; c = a * b; cout << "Line 3 - c The value is " << c << endl ; c = a / b; cout << "Line 4 - c The value is " << c << endl ; c = a % b; cout << "Line 5 - c The value is " << c << endl ; int d = 10; // Test increment、Self-reduction c = d++; cout << "Line 6 - c The value is " << c << endl ; d = 10; // Reassignment c = d--; cout << "Line 7 - c The value is " << c << endl ; return 0; }

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:

OperatorDescriptionexample
==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.

#include <iostream> using namespace std; int main() { int a = 21; int b = 10; int c ; if( a == b ) { cout << "Line 1 - a equal b" << endl ; } else { cout << "Line 1 - a not equal tob" << endl ; } if ( a < b ) { cout << "Line 2 - a Less than b" << endl ; } else { cout << "Line 2 - a Not Less than b" << endl ; } if ( a > b ) { cout << "Line 3 - a more than the b" << endl ; } else { cout << "Line 3 - a no greater than b" << endl ; } /* Change the values of a and b*/ a = 5; b = 20; if ( a <= b ) { cout << "Line 4 - a less than or equal to b" << endl ; } if ( b >= a ) { cout << "Line 5 - b more than The or equal a" << endl ; } return 0; }

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:

OperatorDescriptionexample
&& 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.

#include <iostream> using namespace std; int 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 values of a and b*/ a = 0; b = 10; if ( a && b ) { cout << "Line 3 - Condition is true"<< endl ; } else { cout << "Line 4 - condition Not true"<< endl ; } if ( !(a && b) ) { cout << "Line 5 - Condition is true"<< endl ; } return 0; }

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:

pqp & qp | qp ^ q
00000
01011
11110
10011

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:

OperatorDescriptionInstance
&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

#include <iostream> using namespace std; int 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 The value is " << c << endl ; c = a | b; // 61 = 0011 1101 cout << "Line 2 - c The value is " << c << endl ; c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - c The value is " << c << endl ; c = ~a; // -61 = 1100 0011 cout << "Line 4 - c The value is " << c << endl ; c = a << 2; // 240 = 1111 0000 cout << "Line 5 - c The value is " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - c The value is " << c << endl ; return 0; }

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++:

OperatorDescriptionexample
=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.

#include <iostream> using namespace std; int main() { int a = 21; int c ; c = a; cout << "Line 1 - = Operator instance,c Value = : " <<c<< endl ; c += a; cout << "Line 2 - += Operator instance,c Value = : " <<c<< endl ; c -= a; cout << "Line 3 - -= Operator instance,c Value = : " <<c<< endl ; c *= a; cout << "Line 4 - *= Operator instance,c Value = : " <<c<< endl ; c /= a; cout << "Line 5 - /= Operator instance,c Value = : " <<c<< endl ; c = 200; c %= a; cout << "Line 6 - %= Operator instance,c Value = : " <<c<< endl ; c <<= 2; cout << "Line 7 - <<= Operator instance,c Value = : " <<c<< endl ; c >>= 2; cout << "Line 8 - >>= Operator instance,c Value = : " <<c<< endl ; c &= 2; cout << "Line 9 - &= Operator instance,c Value = : " <<c<< endl ; c ^= 2; cout << "Line 10 - ^= Operator instance,c Value = : " <<c<< endl ; c |= 2; cout << "Line 11 - |= Operator instance,c Value = : " <<c<< endl ; return 0; }

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++.

The
OperatorsDescription
sizeofsizeof operator returns the size of the variable. For example, sizeof(a) will return 4, where a is an integer.
Condition ? X : Yconditional 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 the

operator 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

#include <iostream> using namespace std; int 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 The value is " << e << endl ; e = ((a + b) * c) / d; // (30 * 15 ) / 5 cout << "((a + b) * c) / d The value is " << e << endl ; e = (a + b) * (c / d); // (30) * (15/5) cout << "(a + b) * (c / d) The value is " << e << endl ; e = a + (b * c) / d; // 20 + (150/5) cout << "a + (b * c) / d The value is " << e << endl ; return 0; }

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



Welookups is optimized for learning.© Welookups 2018 -
All Right Reserved and you agree to have read and accepted our term and condition