Latest web development tutorials

C ++ operator

Operator is a symbol to tell the compiler to perform specific mathematical or logical operations. C ++ built a wealth of operators, and provides the following types of operators:

  • Arithmetic operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Miscellaneous Operators

This chapter describes each of arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators.

Arithmetic operators

The following table shows the C ++ support arithmetic operators.

A value of 10 is assumed variable, the variable B is 20, then:

运算符描述实例
+把两个操作数相加 A + B 将得到 30
-从第一个操作数中减去第二个操作数 A - B 将得到 -10
*把两个操作数相乘 A * B 将得到 200
/分子除以分母 B / A 将得到 2
%取模运算符,整除后的余数 B % A 将得到 0
++ 自增运算符 ,整数值增加 1 A++ 将得到 11
-- 自减运算符 ,整数值减少 1 A-- 将得到 9

Examples

Consider the following examples to learn C ++ available arithmetic operators.

Copy and paste the following C ++ program to 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 result:

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 C ++ support relational operators.

A value of 10 is assumed variable, the variable B is 20, then:

运算符描述实例
==检查两个操作数的值是否相等,如果相等则条件为真。 (A == B) 不为真。
!=检查两个操作数的值是否相等,如果不相等则条件为真。 (A != B) 为真。
>检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (A > B) 不为真。
<检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (A < B) 为真。
>=检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (A >= B) 不为真。
<=检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (A <= B) 为真。

Examples

Consider the following examples to learn C ++ available relational operators.

Copy and paste the following C ++ program to 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 result:

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 relationship between C ++ supports logical operators.

Suppose the variable A is 1, the variable B is 0, then:

运算符描述实例
&&称为逻辑与运算符。如果两个操作数都非零,则条件为真。 (A && B) 为假。
||称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (A || B) 为真。
!称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 !(A && B) 为真。

Examples

Consider the following examples to learn C ++ available logical operators.

Copy and paste the following C ++ program to 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 result:

Line 1 - 条件为真
Line 2 - 条件为真
Line 4 - 条件不为真
Line 5 - 条件为真

Bitwise Operators

Bitwise operators acting on the bit, and bit by bit operation. &, | And ^ truth table is as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Suppose if A = 60, and B = 13, and now, in binary form, 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 C ++ support bitwise operators. Suppose the variable A is 60, the value of the variable B 13, then:

运算符描述实例
&如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (A & B) 将得到 12,即为 0000 1100
|如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (A | B) 将得到 61,即为 0011 1101
^如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (A ^ B) 将得到 49,即为 0011 0001
~二进制补码运算符是一元运算符,具有"翻转"位效果。 (~A ) 将得到 -61,即为 1100 0011,2 的补码形式,带符号的二进制数。
<<二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 A << 2 将得到 240,即为 1111 0000
>>二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111

Examples

Consider the following examples to learn C ++ bitwise operators available.

Copy and paste the following C ++ program to 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 result:

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 C ++ supports the assignment operator:

运算符描述实例
=简单的赋值运算符,把右边操作数的值赋给左边操作数 C = A + B 将把 A + B 的值赋给 C
+=加且赋值运算符,把右边操作数加上左边操作数的结果赋值给左边操作数 C += A 相当于 C = C + A
-=减且赋值运算符,把左边操作数减去右边操作数的结果赋值给左边操作数 C -= A 相当于 C = C - A
*=乘且赋值运算符,把右边操作数乘以左边操作数的结果赋值给左边操作数 C *= A 相当于 C = C * A
/=除且赋值运算符,把左边操作数除以右边操作数的结果赋值给左边操作数 C /= A 相当于 C = C / A
%=求模且赋值运算符,求两个操作数的模赋值给左边操作数 C %= A 相当于 C = C % A
<<=左移且赋值运算符 C <<= 2 等同于 C = C << 2
>>=右移且赋值运算符 C >>= 2 等同于 C = C >> 2
&=按位与且赋值运算符 C &= 2 等同于 C = C & 2
^=按位异或且赋值运算符 C ^= 2 等同于 C = C ^ 2
|=按位或且赋值运算符 C |= 2 等同于 C = C | 2

Examples

Consider the following examples to learn C ++ available in the assignment operator.

Copy and paste the following C ++ program to 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 result:

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 the C ++ supports a number of other important operators.

运算符描述
sizeof sizeof 运算符返回变量的大小。例如,sizeof(a) 将返回 4,其中 a 是整数。
Condition ? X : Y 条件运算符 。如果 Condition 为真 ? 则值为 X : 否则值为 Y。
, 逗号运算符会顺序执行一系列运算。整个逗号表达式的值是以逗号分隔的列表中的最后一个表达式的值。
.(点)和 ->(箭头) 成员运算符用于引用类、结构和共用体的成员。
Cast 强制转换运算符把一种数据类型转换为另一种数据类型。例如,int(2.2000) 将返回 2。
& 指针运算符 & 返回变量的地址。例如 &a; 将给出变量的实际地址。
* 指针运算符 * 指向一个变量。例如,*var; 将指向变量 var。

In C ++ operator precedence

Operator precedence determine a combined term of an expression. This affects how an expression calculation. Some operators have a higher priority than other operators, such as addition and subtraction, multiplication and division operator than the operator a higher priority.

For example, x = 7 + 3 * 2, where, x is assigned to 13 instead of 20, because the * operator has higher precedence than +, so first calculate the multiplication 3 * 2, then add 7.

The following table will be listed in order of operator precedence for each operator, having the above higher priority operators appear in the table, with the following lower priority operators appear in the table. In the expression, higher priority operators priority is calculated.

类别  运算符 结合性 
后缀 () [] -> . ++ - -   从左到右 
一元  + - ! ~ ++ - - (type)* & sizeof  从右到左 
乘除  * / % 从左到右 
加减 + -  从左到右 
移位  << >>  从左到右 
关系 < <= > >=  从左到右 
相等  == !=  从左到右 
位与 AND  从左到右 
位异或 XOR  从左到右 
位或 OR  从左到右 
逻辑与 AND &&  从左到右 
逻辑或 OR  ||  从左到右 
条件 ?:  从右到左 
赋值  = += -= *= /= %=>>= <<= &= ^= |= 从右到左 
逗号  从左到右 

Examples

Consider the following examples to learn C ++, operator precedence.

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

There are no comparative difference between brackets and parentheses, which will produce different results. Because (), /, * + and have a different priority, a high priority of operator precedence calculations.

#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 result:

(a + b) * c / d 的值是 90
((a + b) * c) / d 的值是 90
(a + b) * (c / d) 的值是 90
a + (b * c) / d 的值是 50