The C ++ operator is explained in detail in conjunction with the instance.

Source: Internet
Author: User
Tags arithmetic operators

The C ++ operator is explained in detail in conjunction with the instance.
C ++ operators combined with Instances

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

Arithmetic Operators

Relational operators

Logical operators

Bitwise operators

Value assignment operator

Miscellaneous Operators

This chapter describes Arithmetic Operators, Relational operators, logical operators, bitwise operators, value assignment operators, and other operators one by one.

Arithmetic Operators

The following table shows the Arithmetic Operators supported by C ++.

Assume that the value of variable A is 10 and that of variable B is 20. Then:

Operator Description Instance
+ Add 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 denominator B/A will get 2
% Modulo operator, remainder after Division B % A will get 0
++ Auto-increment operator. The integer is increased by 1. A ++ will get 11
-- The auto-subtraction operator reduces the integer by 1. A -- get 9
Instance

See the following example to learn about the Arithmetic Operators available in C ++.

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

Instance
# Include
 
  
Using namespace std; int main () {int a = 21; int B = 10; c = a + B; cout <"the value of Line 1-c is" <c <endl; c = a-B; cout <"the value of Line 2-c is" <c <endl; c = a * B; cout <"the value of Line 3-c is" <c <endl; c = a/B; cout <"the value of Line 4-c is" <c <endl; c = a % B; cout <"the value of Line 5-c is" <c <endl; int d = 10; // test auto-increment and auto-increment c = d ++; cout <"the value of Line 6-c is" <c <endl; d = 10; // re-assign c = d --; cout <"the value of Line 7-c is" <c <endl; return 0 ;}
 

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

Line 1-c value is 31 Line 2-c value is 11 Line 3-c value is 210 Line 4-c value is 2 Line 5-c value is 1 the value of Line 6-c is 10. The value of Line 7-c is 10.
Relational operators

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

Assume that the value of variable A is 10 and that of variable B is 20. Then:

Operator Description Instance
= Check whether the values of the two operands are equal. If they are equal, the condition is true. (A = B) is not true.
! = Check whether the values of the two operands are equal. If they are not equal, the condition is true. (! = B) True.
> Check whether the value of the left operand is greater than the value of the right operand. If yes, the condition is true. (A> B) is not true.
< Check whether the value of the left operand is smaller than the value of the right operand. If yes, the condition is true. (A <B) is true.
> = Check whether the value of the left operand is greater than or equal to the value of the right operand. If yes, the condition is true. (A> = B) is not true.
<= Check whether the value of the left operand is smaller than or equal to the value of the right operand. If yes, the condition is true. (A <= B) is true.
Instance

See the following example to learn about the Relational operators available in C ++.

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

Instance
# Include
 
  
Using namespace std; int main () {int a = 21; int B = 10; int c; if (a = B) {cout <"Line 1-a is equal to B" <endl;} else {cout <"Line 1-a is not equal to B" <endl ;} if (a <B) {cout <"Line 2-a less than B" <endl ;} else {cout <"Line 2-a is not less than B" <endl;} if (a> B) {cout <"Line 3-a greater than B" <endl;} else {cout <"Line 3-a not greater than B" <endl ;} /* change the values 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> =) {cout <"Line 5-B is greater than or equal to a" <endl;} return 0 ;}
 

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

Line 1-a is not equal to bLine 2-a is not less than bLine 3-a is greater than bLine 4-a is less than or equal to bLine 5-B is greater than or equal to
Logical operators

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

Assume that the value of variable A is 1 and that of variable B is 0, then:

Operator Description Instance
&& It is called logic and operator. If both operands are non-zero, the condition is true. (A & B) is false.
| It is called a logic or operator. If either of the two operands is non-zero, the condition is true. (A | B) is true.
! It is called a logical non-operator. Used to reverse the logic status of an operand. If the condition is true, the logical non-operator will make it false. ! (A & B) is true.
Instance

See the following example to learn about the available logical operators in C ++.

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

Instance
# Include
 
  
Using namespace std; int main () {int a = 5; int B = 20; int c; if (a & B) {cout <"Line 1-condition true" <endl;} if (a | B) {cout <"Line 2-condition true" <endl ;} /* change the values of a and B */a = 0; B = 10; if (a & B) {cout <"Line 3-condition true" <endl ;}else {cout <"Line 4-condition not true" <endl ;}if (! (A & B) {cout <"Line 5-the condition is true" <endl;} return 0 ;}
 

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

Line 1-condition true Line 2-condition true Line 4-condition not true Line 5-condition true
Bitwise operators

Bitwise operators act on bitwise AND perform operations one by one. &, | And ^ are shown 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

Assume that if A = 60 and B = 13, they are now in binary format, as shown below:

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 ++. Assume that the value of variable A is 60 and that of variable B is 13. Then:

Operator Description Instance
& If both operands exist, the binary AND operator copies one digit to the result. (A & B) will get 12, that is, 0000 1100
| If it exists in any operand, the binary OR operator copies one digit to the result. (A | B) 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 digit to the result. (A ^ B) 49, that is, 0011 0001
~ The binary complement operator is a unary operator with a "flip" bit effect, that is, 0 is changed to 0. (~ A) The value of-61 is 1100 0011, which is A signed binary complement.
< Binary left shift operator. The value of the left operand moves to 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 the number of digits specified by the right operand to the right. A> 2 get 15, that is, 0000 1111
Instance

See the following example to learn about the bit operators available in C ++.

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

Instance
# Include
 
  
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 value is" <c <endl; c = a | B; // 61 = 0011 1101 cout <"Line 2-c value is" <c <endl; c = a ^ B; // 49 = 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; // 240 = 1111 0000 cout <"Line 5-c value is" <c <endl; c = a> 2; // 15 = 0000 1111 cout <"Line 6-c value is" <c <endl; return 0 ;}
 

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

The value of Line 1-c is 12 Line 2-c. The value of 61 Line 3-c is 49 Line 4-c. The value of-61 Line 5-c is 240 the value of Line 6-c is 15.
Value assignment operator

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

Operator Description Instance
= A simple value 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
+ = Add and assign an operator to assign the result of the right operand plus the left operand to the left operand C + = A is equivalent to C = C +
-= Subtract and assign the value operator. Assign the result of the left operand minus the right operand to the left operand. C-= A is equivalent to C = C-
* = Multiply the right operand by the result of the left operand and assign the value to the left operand. C * = A is equivalent to C = C *
/= Divide the left operand by the right operand and assign the value to the left operand. C/= A is equivalent to C = C/
% = Evaluate the modulo and assign the value operator. Evaluate the modulo value of the two operands to the left operand. C % = A is equivalent to C = C %
<= Left shift and value assignment operator C <= 2 is equivalent to C = C <2
>>= Right Shift and value assignment operator C> = 2 is equivalent to C = C> 2
& = Bitwise AND value assignment operator C & = 2 is equivalent to C = C & 2
^ = Bitwise XOR and value assignment operator C ^ = 2 is equivalent to C = C ^ 2
| = Bitwise OR and value assignment operator C | = 2 is equivalent to C = C | 2
Instance

See the following example to learn about the value assignment operators available in C ++.

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

Instance
# Include
 
  
Using namespace std; int main () {int a = 21; int c; c = a; cout <"Line 1-= Operator instance, c value =:" <
  
   
>=2; cout <"Line 8-> = Operator instance, c value =:" <
   
    

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

Line 1-= Operator instance, c value = 21 Line 2-+ = Operator instance, c value = 42 Line 3-= Operator instance, the value of c is 21 Line 4-* = Operator instance. The value of c is 441 Line 5-/= Operator instance. The value of c is 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 Operators

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

Operator Description
Sizeof The sizeof operator returns the variable size. For example, sizeof (a) returns 4, where a is an integer.
Condition X: Y Conditional operators. 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 the comma-separated list.
. (Points) and-> (arrows) The member operator is used to reference members of classes, structures, and shared bodies.
Cast The force conversion operator converts one data type to another. For example, int (2.2000) returns 2.
& Pointer operator & Return variable address. For example, & a; the actual address of the variable is given.
* The pointer operator * points to a variable. For example, * var; points to the variable var.
Operator priority in C ++

Operator priority determines the combination of items in the expression. This affects how an expression is computed. Some operators have a higher priority than other operators. For example, multiplication and division operators have a higher priority than addition and subtraction operators.

For example, x = 7 + 3*2. Here, x is assigned to 13 rather than 20. Because the operator * has a higher priority than +, multiplication 3*2 is first calculated, then add 7.

The following table lists operators from high to low by operator priority. Operators with higher priority appear on the table, and operators with lower priority appear under the table. In expressions, operators with higher priority are preferentially calculated.

Category Operator Associativity
Suffix () []->. ++ -- Left to right
RMB 1 + -! ~ ++--(Type) * & sizeof From right to left
Multiplication and division */% Left to right
Addition and subtraction +- Left to right
Shift <> Left to right
Link <=> = Left to right
Equal =! = Left to right
Bit AND & Left to right
Bitwise OR XOR ^ Left to right
Bit OR | Left to right
Logic AND && Left to right
Logic OR | Left to right
Condition : From right to left
Assignment = + =-= * =/=%=>=<<<=<=^= | = From right to left
Comma , Left to right
Instance

See the following example to learn about the priority of operators in C ++.

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

The difference between brackets and those without parentheses produces different results. Because (),/, *, and + have different priorities, high-priority operators will be given priority.

Instance
# Include
     
      
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) * The value of c/d is" <e <endl; e = (a + B) * c)/d; // (30*15)/5 cout <"(a + B) * c) the value of/d is "<e <endl; e = (a + B) * (c/d); // (30) * (15/5) the value of cout <"(a + B) * (c/d) is" <e <endl; e = a + (B * c)/d; // The value of 20 + (150/5) cout <"a + (B * c)/d is" <e <endl; return 0 ;}
     

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

(A + B) * The value of c/d is 90 (a + B) * c)/d is 90 (a + B) * (c/d) the value of 90a + (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.