Cplusplus 简明教程

Operators in C++

运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作。C++ 拥有丰富的内置运算符,并提供了以下类型的运算符 −

  1. Arithmetic Operators

  2. Relational Operators

  3. Logical Operators

  4. Bitwise Operators

  5. Assignment Operators

  6. Misc Operators

本章将逐个审查算术、关系、逻辑、按位、赋值和其他运算符。

Arithmetic Operators

以下是由 C++ 语言支持的算术运算符 −

假设变量 A 为 10,变量 B 为 20,那么 −

Operator

Description

Example

+

Adds two operands

A + B 会给出 30

-

从第一个操作数中减去第二个操作数

A - B 会给出 -10

*

Multiplies both operands

A * B 会给出 200

/

Divides numerator by de-numerator

B / A 会给出 2

%

模运算符,整数除法后的余数

B % A 会给出 0

++

Increment operator ,将整数值加一

A++ will give 11

 — 

Decrement operator ,将整数值减一

A-- will give 9

Relational Operators

C++ 语言支持以下关系运算符:

假设变量 A 为 10,变量 B 为 20,那么 −

Operator

Description

Example

==

检查两个操作数的值是否相等,如果相等,则条件变为真。

(A == B) 不为真。

!=

检查两个操作数的值是否相等,如果值不相等,则条件变为真。

(A != B) 为真。

>

检查左操作数的值是否大于右操作数的值,如果大于,则条件变为真。

(A > B) 不为真。

<

检查左操作数的值是否小于右操作数的值,如果小于,则条件变为真。

(A < B) 为真。

>=

检查左操作数的值是否大于或等于右操作数的值,如果大于或等于,则条件变为真。

(A >= B) 不为真。

检查左操作数的值是否小于或等于右操作数的值,如果小于或等于,则条件变为真。

(A ⇐ B) 为真。

Logical Operators

C++ 语言支持以下逻辑运算符:

假设变量 A 为 1,变量 B 为 0,则 −

Operator

Description

Example

&&

称为逻辑 AND 运算符。如果两个运算数都非零,条件变为真。

(A && B) 为假。

称为逻辑 OR 运算符。如果任何一个运算数非零,条件变为真。

(A

B) is true.

!

称为逻辑 NOT 运算符。用于反转运算数的逻辑状态。如果条件为真,逻辑 NOT 运算符将变为假。

Bitwise Operators

位运算符作用于位,并执行逐位运算。用于 &、| 和 ^ 的真值表如下 −

p

q

p &

p

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

假设如果 A = 60;并且 B = 13;现在以二进制格式,它们将如下所示 −

A = 0011 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

The Bitwise operators supported by C++ language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then −

link:../cplusplus/cpp_bitwise_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|&|Binary AND Operator copies a bit to the result if it exists in both operands.|(A & B) will give 12 which is 0000 1100
|||Binary OR Operator copies a bit if it exists in either operand.|(A | B) will give 61 which is 0011 1101
|^|Binary XOR Operator copies the bit if it is set in one operand but not both.|(A ^ B) will give 49 which is 0011 0001
|~|Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.|(~A ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
|<<|Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.|A << 2 will give 240 which is 1111 0000
|>>|Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.|A >> 2 will give 15 which is 0000 1111
|===


=== Assignment Operators

There are following assignment operators supported by C++ language −

link:../cplusplus/cpp_assignment_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|=|Simple assignment operator, Assigns values from right side operands to left side operand.|C = A + B will assign value of A + B into C
|+=|Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand.|C += A is equivalent to C = C + A
|-=|Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand.|C -= A is equivalent to C = C - A
|*=|Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand.|C *= A is equivalent to C = C * A
|/=|Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand.|C /= A is equivalent to C = C / A
|%=|Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand.|C %= A is equivalent to C = C % A
|<<=|Left shift AND assignment operator.|C <<= 2 is same as C = C << 2
|>>=|Right shift AND assignment operator.|C >>= 2 is same as C = C >> 2
|&=|Bitwise AND assignment operator.|C &= 2 is same as C = C & 2
|^=|Bitwise exclusive OR and assignment operator.|C ^= 2 is same as C = C ^ 2
||=|Bitwise inclusive OR and assignment operator.|C |= 2 is same as C = C | 2
|===


=== Misc Operators

The following table lists some other operators that C++ supports.
[%autowidth]
|===

|Sr.No|Operator & Description
|1|*sizeof*
link:../cplusplus/cpp_sizeof_operator.html[sizeof operator] returns the size of a variable. For example, sizeof(a), where ‘a’ is integer, and will return 4.

|2|*Condition ? X : Y*
link:../cplusplus/cpp_conditional_operator.html[Conditional operator (?)]. If Condition is true then it returns value of X otherwise returns value of Y.

|3|*,*
link:../cplusplus/cpp_comma_operator.html[Comma operator] causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.

|4|*. (dot) and -> (arrow)*
link:../cplusplus/cpp_member_operators.html[Member operators] are used to reference individual members of classes, structures, and unions.

|5|*Cast*
link:../cplusplus/cpp_casting_operators.html[Casting operators] convert one data type to another. For example, int(2.2000) would return 2.

|6|*&*
link:../cplusplus/cpp_pointer_operators.html[Pointer operator &] returns the address of a variable. For example &a; will give actual address of the variable.

|7|***
link:../cplusplus/cpp_pointer_operators.html[Pointer operator *] is pointer to a variable. For example *var; will pointer to a variable var.

|===


=== Operators Precedence in C++

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator −

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

link:../cplusplus/cpp_operators_precedence.html[Show Examples]
[%autowidth]
|===

|Category |Operator |Associativity
|Postfix |() [] -> . ++ - -  |Left to right
|Unary |+ - ! ~ ++ - - (type)* & sizeof |Right to left
|Multiplicative  |* / % |Left to right
|Additive  |+ - |Left to right
|Shift  |<< >> |Left to right
|Relational  |< <= > >= |Left to right
|Equality  |== != |Left to right
|Bitwise AND |& |Left to right
|Bitwise XOR |^ |Left to right
|Bitwise OR || |Left to right
|Logical AND |&& |Left to right
|Logical OR ||| |Left to right
|Conditional |?: |Right to left
|Assignment |= += -= *= /= %=>>= <<= &= ^= |= |Right to left
|Comma |, |Left to right
|===