Operators and operator Precedence in C + + summary _c language

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. C + + has a rich operator built into it and provides the following types of operators:

    • Arithmetic operators
    • Relational operators
    • logical operators
    • Bitwise operators
    • Assignment operator
    • Miscellaneous operators

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

Arithmetic operators

The following table shows all the arithmetic operators supported by C + +.
Assuming the value of variable A is 10, and the value of variable B is 20, then:

Operator Describe Instance
+ Adds 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
/ Numerator divided by the denominator B/A will get 2
% Modulo operator, the remainder of the divide B% A will get 0
++ Self-increasing operator with integer value increased by 1 a++ will get 11
-- Self-subtraction operator, integer value reduced by 1 a--will get 9

Instance

Take a look at the example below to find out all the arithmetic operators available in C + +.
Copy and paste the following C + + program into the Test.cpp file and compile and run the program.

#include <iostream>
using namespace std;

Main ()
{
 int a =;
 int B = Ten;
 int C;

 c = a + B;
 cout << "line 1-c value is" << c << Endl;
 c = a-b;
 cout << "line 2-c value is" << c << Endl;
 c = A * b;
 cout << "line 3-c value is" << c << Endl;
 c = A/b;
 cout << "line 4-c value is" << c << Endl;
 c = a% B;
 cout << "line 5-c value is" << c << Endl;
 c = a++;
 cout << "line 6-c value is" << c << Endl;
 c = a--;
 cout << "line 7-c value is" << c << Endl;
 return 0;
}

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

Line 1-c is the value of line 2-c is line 3-c value is 210 line 4-c value is 2 line 5-c
The value is 1 line
6- The value of C is the value of line
7-c is 22

Relational operators

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:

Operator Describe Instance
== Checks whether the values of two operands are equal, and if they are equal, the condition is true. (A = = B) is not true.
!= Checks whether the values of two operands are equal, and if not, the condition is true. (A!= B) is true.
> Check that the value of the left operand is greater than the value of the right operand, and if so, the condition is true. (A > B) is not true.
< Check the value of the left operand to be less than the right operand, and if so, the condition is true. (A < B) is true.
>= Check that the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. (A >= B) is not true.
<= Check that the value of the left operand is less than or equal to the value of the right operand, and if so, the condition is true. (A <= B) is true.

Instance

Take a look at the example below to see all the relational operators available in C + +.
Copy and paste the following C + + program into the Test.cpp file and compile and run the program.

#include <iostream>
using namespace std;

Main ()
{
 int a =;
 int B = Ten;
 int C;

 if (a = = b)
 {
  cout << "line 1-a equals B" << Endl;
 }
 else
 {
  cout << "line 1-a not equal to B" << 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 greater than B" << Endl;
 }
 else
 {
  cout << "line 3-a not more than B" << Endl;
 }
 /* Change the value of a and B */
 a = 5;
 b =;
 if (a <= b)
 {
  cout << "line 4-a less than or equal to B" << Endl;
 }
 if (b >= a)
 {
  cout << "line 5-b greater than or equal to B" << Endl;
 }
 return 0;
}

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

Line 1-a Not equal to B line
2-a not less than B line
3-a greater than B line
4-a less than or equal to B line
5-b greater than or equal to B

logical operators

The following table shows all the relational logical operators supported by C + +.
Assuming the value of variable A is 1, and the value of variable B is 0, then:

Operator Describe Instance
&& Called Logic and operators. If all two operands are non-zero, the condition is true. (A && B) is false.
|| Called the logic or operator. If there are any nonzero zeros in the two operands, the condition is true. (A | | B) is true.
! is called a logical non operator. Used to reverse the logical state of the operand. If the condition is true, the logical non-operator makes it false. ! (A && B) is true.

Instance

Take a look at the example below to find out all the logical operators available in C + +.
Copy and paste the following C + + program into the Test.cpp file and compile and run the program.

#include <iostream>
using namespace std;

Main ()
{
 int a = 5;
 int b =;
 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 = Ten;
 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 results:

Line 1-The condition is true line
2-The condition is true line
3-The condition is not true line
4-The condition is true

Bitwise operators

The bitwise operator acts on a bit and performs the operation bit-by-byte. &, | and ^ 's truth table looks like this:

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 that if A = 60 and B = 13 are now represented 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 bitwise operators supported by C + +. Assuming the value of variable A is 60, and the value of variable B is 13, then:

Operator Describe Instance
& If the same exists in two operands, the binary and operator replicates one to the result. (A & B) will receive 12, that is 0000 1100
| If present in any operand, the binary OR operator replicates one to the result. (A | B) will receive 61, that is 0011 1101
^ If present in one of the operands but not in the same two operands, the binary XOR or operator replicates one to the result. (A ^ B) will be 49, that is 0011 0001
~ The binary complement operator is a unary operator with a "flip" bit effect. (~a) will get 61, that is, 1100 0011,2 complement form, signed binary number.
<< Binary left-shift operator. The value of the left operand shifts the number of digits specified by the right-hand operand. A << 2 will get 240, that is 1111 0000
>> Binary right shift operator. The value of the left operand shifts the number of digits specified by the right-hand operand to the right. A >> 2 will get 15, that is 0000 1111

Instance

Take a look at the example below to find out all the bitwise operators available in C + +.
Copy and paste the following C + + program into the Test.cpp file and compile and run the program.

#include <iostream>
using namespace std;

Main ()
{
 unsigned int a = 0011 1100 
 unsigned int b = n; = 0000 1101
 int c = 0;   

 c = A & B;    The
 value of 0000 1100 cout << "line 1-c is" << c << Endl;

 c = A | b;    1101 = 0011
 cout << "line 2-c value is" << c << Endl;

 c = a ^ B;    0011 = 0001
 cout << "line 3-c value is" << c << Endl;

 c = ~a;    -61 = 1100 0011
 cout << "line 4-c value is" << c << Endl;

 c = a << 2;   The value of the 1111 0000
 cout << "line 5-c is" << c << Endl;

 c = a >> 2;   The
 value of 0000 1111 cout << "line 6-c is" << c << Endl;

 return 0;
}

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

The value of line 1-c is the value of line 2-c is the value of line 3-c is the value of line
4-c is -61 line 5-c is
the value of
the ' line ' line 6-c The value is 15

Assignment operator

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

Operator Describe Instance
= A simple assignment operator that assigns the value of the right-hand operand to the left-hand operand c = A + B assigns the value of A + B to c
+= Plus and assignment operators, assigning the right-hand operand to the left-hand operand with the result of the left-hand operand C + + a equals c = C + A
-= Minus and assignment operators, assigning the left-hand operand minus the right-hand operand to the left-hand operand c-= A equals c = c-a
*= Multiply and assign operators, multiply the right-hand operand by the result of the left-hand operand to the left operand c = a equals c = C A
/= In addition to the assignment operator, assigns the left-hand operand to the left operand by dividing the result of the right-hand operand C/= A equals C = c/a
%= Modulo and assignment operators, asking for modulo assignment of two operands to left operand C%= a equals c = c% A
<<= Move left and assignment operator C <<= 2 equals c = C << 2
>>= Move right and assignment operator C >>= 2 equals c = C >> 2
&= Bitwise AND and assignment operators C &= 2 equals c = C & 2
^= Bitwise XOR and Assignment operators C ^= 2 equals c = c ^ 2
|= bitwise OR and assignment operators C |= 2 equals c = C | 2

Instance

Take a look at the example below to learn all the assignment operators available in C + +.
Copy and paste the following C + + program into the Test.cpp file and compile and run the program.

#include <iostream> using namespace std;
 Main () {int a = 21;

 int C;
 c = A;

 cout << "Line 1-= operator instance, value of c =:" <<c<< Endl;
 C + A;

 cout << "Line 2-+ = operator instance, value of c =:" <<c<< Endl;
 c-= A;

 cout << "Line 3-= operator instance, value of c =:" <<c<< Endl;
 C *= A;

 cout << "Line 4-*= operator instance, value of c =:" <<c<< Endl;
 C/= A;

 cout << "Line 5-/= operator instance, value of c =:" <<c<< Endl;
 c = 200;
 C%= A;

 cout << "line 6-%= operator instance, value of c =:" <<c<< Endl;
 C <<= 2;

 cout << "Line 7-<<= operator instance, value of c =:" <<c<< Endl;
 C >>= 2;

 cout << "Line 8->>= operator instance, value of c =:" <<c<< Endl;
 C &= 2;

 cout << "Line 9-&= operator instance, value of c =:" <<c<< Endl;
 C ^= 2;

 cout << "Line 10-^= operator instance, value of c =:" <<c<< Endl;
 C |= 2; cout << "line 11-|= operator instance, value of c =:" <<c<< ENDL;
return 0;

 }

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

Line 1-= operator instance, the value of C = Line
2-+ = operator instance, the value of C = Line
3-= operator instance, C's value = Line
4-*= operator instance, C's value = 44 1 line
5-/= operator instance, C's value = line
6-%= operator instance, C's value = line
7-<<= operator instance, C's value = line
8-> >= operator instance, the value of C = one 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 of the other important operators supported by C + +.

operator Description
sizeof The sizeof operator returns the size of the variable. For example, sizeof (a) returns 4, where A is an integer.
Condition? X:y The conditional operator. What if Condition is true? The value is X: 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 a comma-delimited list.
. (points) and-> (arrows) A member operator is used to refer to a member of a class, struct, and shared body.
Cast The cast operator converts a data type to another data type. For example, int (2.2000) will return 2.
& Pointer operator & return the address of a variable. For example &a; The actual address of the variable is given.
* The pointer operator * points to a variable. For example, *var; will point to variable var.

operator Precedence in C + +

The precedence of an operator determines the combination of items in an expression. This affects how an expression is evaluated. Some operators have higher precedence than other operators, for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.
For example x = 7 + 3 2, where x is assigned 13 instead of 20 because the operator has a higher precedence than +, so first compute the multiplication 3*2 and then add 7.
The following table lists the operators by operator precedence from highest to lowest, with higher precedence operators appearing above the table, with lower precedence operators appearing below the table. In an expression, higher-priority operators are evaluated preferentially.

Category Operator Combination of
Suffix () []->. ++ - - From left to right
One dollar + - ! ~ + +--(type) * & sizeof From right to left
Multiplication * / % 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 & From left to right
Bitwise exclusive OR XOR ^ From left to right
Bit OR OR | From left to right
Logic and && From left to right
Logical OR OR || From left to right
Conditions ?: From right to left
assigning values = + = = *=/=%=>>= <<= &= ^= |= From right to left
Comma , From left to right

Instance

Consider the following example to learn about the precedence of operators in C + +.
Copy and paste the following C + + program into the Test.cpp file and compile and run the program.
Compare the differences between parentheses and no parentheses, which will produce different results. Because (),/, *, and + have different priorities, high priority operators will be evaluated first.

#include <iostream>
using namespace std;

Main ()
{
 int a =;
 int B = Ten;
 int c =;
 int d = 5;
 int e;

 E = (A + b) * C/D;  (*)/5
 cout << "(A + B) * C/D value is" << e << Endl;

 E = ((A + b) * c)/D; (*)/5
 cout << "((A + b) * C)/d is" << e << Endl;

 E = (A + b) * (C/D); (d) * (15/5)
 cout << "(A + B) * (C/D) The value is" << e << Endl;

 E = a + (b * c)/D;  The value of + (150/5)
 cout << "A + (b * c)/d is" << e << Endl;

 return 0;
}

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

(A + B) * The value of C/D (
(A + b) * C)/d is the value of (
A + b) * (C/D) is the value of
A + (b * c)/d is 50

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.