C++ operators and expressions

Operator

Operators are symbols that tell the compiler to perform specific mathematical or logical operations.
C++ has a wealth of built-in operators, and provides the following types of operators:

  • Arithmetic Operator
  • Relational operator
  • Logical Operators
  • Bit operator
  • Assignment operator
  • Miscellaneous operators

expression

In the program, operators are used to manipulate data. These data are also called operands. The formulas that use operators to connect operands are called expressions.
The expression has the following characteristics

  • Constants and variables are expressions
  • The type of operator corresponds to the type of expression
  • Every expression has its own value

Arithmetic Operator

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:

Operator description Instance
+ Add two operands A + B will give 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
% Modulus operator, remainder after division B% A will get 0
++ Increment operator, integer value increases by 1 A++ will get 11
Decrement operator, the integer value is decreased by 1 A-- will get 9

Relational operator

**The table below shows the relational operators supported by C++.
Assuming the value of variable A is 10 and the value 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 becomes true. (A == B) is not true.
!= Check whether the values ​​of the two operands are equal, if not equal then the condition becomes true. (A != B) is true.
> Check if the value of the left operand is greater than the value of the right operand, if so, the condition becomes true. (A> B) is not true.
< Check whether the value of the left operand is less than the value of the right operand, if it is, the condition becomes true. (A <B) is true.
>= Check if the value of the left operand is greater than or equal to the value of the right operand, if it is, the condition becomes true. (A >= B) is not true.
<= Check whether the value of the left operand is less than or equal to the value of the right operand, if it is, the condition becomes true. (A <= B) is true. **

Logical Operators

The following table shows 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:

Operator description Instance
&& It is called the logical AND operator. If both operands are non-zero, then the condition becomes true. (A && B) is false.
|| Called the logical OR operator. If either of the two operands is non-zero, the condition becomes true.
! Called the logical negation operator. Used to reverse the logic state of the operand. If the condition is true, the logical negation operator will make it false. !(A && B) is true.

Bit operator

Bit operators act on bits and perform operations bit by bit. The truth table of &, | and ^ is 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

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:

Operator description Instance
& If it exists in both operands, the binary AND operator copies one bit to the result. (A & B) will get 12, which is 0000 1100
| If it exists in any operand, the binary OR operator copies one bit to the result. (A |B) will get 61, which is 0011 1101
^ If it exists in one of the operands but does not exist in both operands at the same time, the binary XOR operator copies one bit to the result. (A ^ B) will get 49, which is 0011 0001
~ The two's complement operator is a unary operator with a "flip" bit effect, that is, 0 becomes 1 and 1 becomes 0. (~A) will get -61, which is 1100 0011, the complement of a signed binary number.
<< Binary shift left operator. The value of the left operand is shifted to the left by the number of bits specified by the right operand. A << 2 will get 240, which is 1111 0000
>> 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 A >> 2 将得到 15,即为 0000 1111

赋值运算符

下表列出了 C++ 支持的赋值运算符:

运算符 描述 实例
= 简单的赋值运算符,把右边操作数的值赋给左边操作数 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

杂项运算符

下表列出了 C++ 支持的其他一些重要的运算符。

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

C++ 中的运算符优先级

运算符的优先级确定表达式中项的组合。这会影响到一个表达式如何计算。某些运算符比其他运算符有更高的优先级,例如,乘除运算符具有比加减运算符更高的优先级。

例如 x = 7 + 3 * 2,在这里,x 被赋值为 13,而不是 20,因为运算符 * 具有比 + 更高的优先级,所以首先计算乘法 3*2,然后再加上 7。

下表将按运算符优先级从高到低列出各个运算符,具有较高优先级的运算符出现在表格的上面,具有较低优先级的运算符出现在表格的下面。在表达式中,较高优先级的运算符会优先被计算。

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

Guess you like

Origin blog.csdn.net/yasuofenglei/article/details/108477658