Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Operators

C++ Operators

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

This chapter will introduce arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, and other operators one by one.

Arithmetic operators

The table below shows the arithmetic operators supported by C++.

Assume variable A has the value 10 and variable B has the value 20, then:

operator Description Example
+ Adds the two operands A + B will give 30
- Subtracts the second operand from the first operand A - B will give -10
* Multiplies the two operands A * B will give 200
/ Divides numerator by denominator B / A will give 2
% Modulus operator, the remainder after integer division B % A will give 0
++ Increment operator, increments the integer value by 1 A++ will give 11
-- Decrement operator, decrements the integer value by 1 A-- will give 9

Example

See the example below to learn about the arithmetic operators available in C++.

Copy and paste the C++ program below into the test.cpp file, then compile and run the program.

Example

#include

<

iostream

using

namespace

std

;

int

main

(

)

{

int

a

=

21

;

int

b

=

10

;

int

c

;

c

=

a

b

;

cout

<<

"

Line 1 - Value of c is

"

<<

c

<<

endl

;

c

=

a

b

;

cout

<<

"

Line 2 - Value of c is

"

<<

c

<<

endl

;

c

=

a

b

;

cout

<<

"

Line 3 - Value of c is

"

<<

c

<<

endl

;

c

=

a

/

b

;

cout

<<

"

Line 4 - Value of c is

"

<<

c

<<

endl

;

c

=

a

%

b

;

cout

<<

"

Line 5 - Value of c is

"

<<

c

<<

endl

;

int

d

=

10

;

// Test increment, decrement

c

=

d

++;

cout

<<

"

Line 6 - Value of c is

"

<<

c

<<

endl

;

d

=

10

;

// Reassign

c

=

d

--;

cout

<<

"

Line 7 - Value of c is

"

<<

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 的值是 10

Line 7 - c 的值是 10

Relational operators

The table below shows the relational operators supported by C++.

Assume variable A has the value 10 and variable B has the value 20, then:

operator Description Example
== Checks whether the values of the two operands are equal; if equal, the condition is true. (A == B) is not true.
!= Checks whether the values of the two operands are equal; if not equal, the condition is true. (A != B) is true.
> Checks whether the value of the left operand is greater than the value of the right operand; if so, the condition is true. (A > B) is not true.
< Checks whether the value of the left operand is less than the value of the right operand; if so, the condition is true. (A < B) is true.
>= Checks whether the value of the left operand is greater than or equal to the value of the right operand; if so, the condition is true. (A >= B) is not true.
<= Checks whether the value of the left operand is less than or equal to the value of the right operand; if so, the condition is true. (A <= B) is true.

Example

See the example below to learn about the relational operators available in C++.

Copy and paste the C++ program below into the test.cpp file, then compile and run the program.

Example

#include

<

iostream

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 is less than b

"

<<

endl

;

}

else

{

cout

<<

"

Line 2 - a is not less than b

"

<<

endl

;

}

if

(

a

b

)

{

cout

<<

"

Line 3 - a is greater than b

"

<<

endl

;

}

else

{

cout

<<

"

Line 3 - a is 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

=

a

)

{

cout

<<

"

Line 5 - b is greater than or equal to 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 table below shows the relational and logical operators supported by C++.

Assume variable A has the value 1 and variable B has the value 0, then:

operator Description Example
&& is called the logical AND operator. If both operands are true, the condition is true. (A && B) is false.
|| is called the logical OR operator. If either of the two operands is true, the condition is true. (A || B) is true.
! is called the logical NOT operator. It reverses the logical state of the operand; if the condition is true, the logical NOT operator makes it false. !(A && B) is true.

Example

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

Copy and paste the C++ program below into the test.cpp file, then compile and run the program.

Example

#include

<

iostream

using

namespace

std

;

int

main

(

)

{

int

a

=

5

;

int

b

=

20

;

int

c

;

if

(

a

&&

b

)

{

cout

<<

"

Line 1 - Condition is true

"

<<

endl

;

}

if

(

a

||

b

)

{

cout

<<

"

Line 2 - Condition is true

"

<<

endl

;

}

/*

Change the values of a and b

*/

a

=

0

;

b

=

10

;

if

(

a

&&

b

)

{

cout

<<

"

Line 3 - Condition is true

"

<<

endl

;

}

else

{

cout

<<

"

Line 4 - Condition is 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 result:



Line 1 - 条件为真

Line 2 - 条件为真

Line 4 - 条件不为真

Line 5 - 条件为真

Bitwise operators

Bitwise operators act on bits and perform operations bit by bit. The truth tables for &, |, and ^ are shown below:

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 A = 60 and B = 13; 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 table below shows the bitwise operators supported by C++. Assume variable A has the value 60 and variable B has the value 13, then:

operator Description Example
&

Bitwise AND operation, performs the "AND" operation bit by bit. The rule is:

0&0=0;

0&1=0;
1&0=0;
1&1=1; | (A & B) will give 12, that is 0000 1100 | | | |

Bitwise OR operator, performs the "OR" operation bit by bit. The rule is:

0|0=0;
0|1=1;
1|0=1;
1|1=1;
| (A | B) will give 61, that is 0011 1101 | | ^ |

XOR operator, performs the "XOR" operation bit by bit. The rule is:

0^0=0;
0^1=1;
1^0=1;
1^1=0;
| (A ^ B) will give 49, that is 0011 0001 | | ~ |

Bitwise NOT operator, performs the "NOT" operation bit by bit. The rule is:

~1=-2;
~0=-1;
| (~A ) yields -61, i.e. 1100 0011, the two's complement of a signed binary number. | | << | Binary left shift operator. Shifts all bits of an operand to the left by a number of positions (bits on the left are discarded, 0 filled on the right). | A << 2 yields 240, i.e. 1111 0000 | | >> | Binary right shift operator. Shifts all bits of a number to the right by a number of positions; for positive numbers fill 0 on the left, for negative numbers fill 1, discarding the bits on the right. | A >> 2 yields 15, i.e. 0000 1111 |

Example

See the example below to learn about the bitwise operators available in C++.

Copy and paste the C++ program below into the test.cpp file, then compile and run the program.

Example

#include

<

iostream

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 - Value of c is

"

<<

c

<<

endl

;

c

=

a

|

b

;

// 61 = 0011 1101

cout

<<

"

Line 2 - Value of c is

"

<<

c

<<

endl

;

c

=

a

^

b

;

// 49 = 0011 0001

cout

<<

"

Line 3 - Value of c is

"

<<

c

<<

endl

;

c

= ~

a

;

// -61 = 1100 0011

cout

<<

"

Line 4 - Value of c is

"

<<

c

<<

endl

;

c

=

a

<<

2

;

// 240 = 1111 0000

cout

<<

"

Line 5 - Value of c is

"

<<

c

<<

endl

;

c

=

a

2

;

// 15 = 0000 1111

cout

<<

"

Line 6 - Value of c is

"

<<

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 table below lists the assignment operators supported by C++:

operator Description Example
= Simple 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 operator, assigns the result of adding the right operand to the left operand to the left operand C += A is equivalent to C = C + A
-= Subtract-and-assign operator, assigns the result of subtracting the right operand from the left operand to the left operand C -= A is equivalent to C = C - A
*= Multiply-and-assign operator, assigns the result of multiplying the right operand by the left operand to the left operand C *= A is equivalent to C = C * A
/= Divide-and-assign operator, assigns the result of dividing the left operand by the right operand to the left operand C /= A is equivalent to C = C / A
%= Modulus-and-assign operator, assigns the modulus of the two operands to the left operand C %= A is equivalent to C = C % A
<<= Left-shift-and-assign operator C <<= 2 is equivalent to C = C << 2
>>= Right-shift-and-assign operator C >>= 2 is the same as C = C >> 2
&= Bitwise AND-and-assign operator C &= 2 is the same as C = C & 2
^= Bitwise XOR-and-assign operator C ^= 2 is the same as C = C ^ 2
|= Bitwise OR-and-assign operator C |= 2 is the same as C = C | 2

Example

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

Copy and paste the C++ program below into the test.cpp file, then compile and run the program.

Example

#include

<

iostream

using

namespace

std

;

int

main

(

)

{

int

a

=

21

;

int

c

;

c

=

a

;

cout

<<

"

Line 1 - = operator example, value of c = :

"

<<

c

<<

endl

;

c

+=

a

;

cout

<<

"

Line 2 - += operator example, value of c = :

"

<<

c

<<

endl

;

c

-=

a

;

cout

<<

"

Line 3 - -= operator example, value of c = :

"

<<

c

<<

endl

;

c

*=

a

;

cout

<<

"

Line 4 - *= operator example, value of c = :

"

<<

c

<<

endl

;

c

/=

a

;

cout

<<

"

Line 5 - /= operator example, value of c = :

"

<<

c

<<

endl

;

c

=

200

;

c

%=

a

;

cout

<<

"

Line 6 - %= operator example, value of c = :

"

<<

c

<<

endl

;

c

<<=

2

;

cout

<<

"

Line 7 - <<= operator example, value of c = :

"

<<

c

<<

endl

;

c

=

2

;

cout

<<

"

Line 8 - >>= operator example, value of c = :

"

<<

c

<<

endl

;

c

&=

2

;

cout

<<

"

Line 9 - &= operator example, value of c = :

"

<<

c

<<

endl

;

c

^=

2

;

cout

<<

"

Line 10 - ^= operator example, value of c = :

"

<<

c

<<

endl

;

c

|=

2

;

cout

<<

"

Line 11 - |= operator example, value of 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 table below lists some other important operators supported by C++.

operator Description
sizeof sizeof operatorReturns the size of a variable. For example, sizeof(a) will return 4, where a is an integer.
Condition ? X : Y Conditional operator. If Condition is true ? then the value is X : otherwise the value is Y.
, Comma operatorExecutes a series of operations in sequence. The value of the entire comma expression is the value of the last expression in the comma-separated list.
. (dot) and -> (arrow) member operatorsused to refer to members of classes, structures, and unions.
Cast Cast operatorConverts one data type to another. For example, int(2.2000) will return 2.
& Pointer operator & Returns the address of a variable. For example, &a; will give the actual address of the variable.
* Pointer operator * Points to a variable. For example, *var; will point to the variable var.

Operator Precedence in C++

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Some operators have higher precedence than others; for example, the multiplication and division operators have higher precedence than the addition and subtraction operators.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20, because the * operator has higher precedence than +, so the multiplication 3*2 is computed first, then 7 is added.

The table below lists the operators from highest to lowest precedence. Operators with higher precedence appear at the top of the table, and those with lower precedence appear at the bottom. In an expression, operators with higher precedence are evaluated first.

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 

Example

See the example below to learn about operator precedence in C++.

Copy and paste the C++ program below into the test.cpp file, then compile and run the program.

Compare the difference with and without parentheses; this produces different results. Because (), /, *, and + have different precedence, operators with higher precedence are evaluated first.

Example

#include

<

iostream

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

<<

"

The value of (a + b) * c / d is

"

<<

e

<<

endl

;

e

=

(

(

a

b

)

c

)

/

d

;

// (30 * 15 ) / 5

cout

<<

"

The value of ((a + b) * c) / d is

"

<<

e

<<

endl

;

e

=

(

a

b

)

(

c

/

d

)

;

// (30) * (15/5)

cout

<<

"

The value of (a + b) * (c / d) is

"

<<

e

<<

endl

;

e

=

a

(

b

c

)

/

d

;

// 20 + (150/5)

cout

<<

"

The value of a + (b * c) / d is

"

<<

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