Chapter 02 Description of Simple Data
4. Operator
Operator describes basic operation on data, which is also called operand(s).
Operands can be both variables or constants or expressions that evaluate to a value.
In C++, some operator will change the value of operands, such as
++
--
=
. This is called side effect.
4.1 Arithmetic Operator
name | symbol |
---|---|
add/ positive | + |
minus/ negative | - |
multiply | * |
divide | / |
self-increase | ++ |
self-decrease | -- |
1. About /
- When the operands of
/
are both integer,/
will turn into//
.
2. About ++
/--
-
++
and--
can both be put before or behind the operand, there will be some difference if the whole expression is used as an operand of other operator. -
++
/--
put before the operand means first self-increase/self-decrease then use;++
/--
put behind the operand means first use then self-increase/self-decrease. -
++
/--
not only evaluates to a value but also change the value of the operand, so they are operator with side effects.
The data type of the return value of an arithmetic operator is usually the same as the operand(s).
The problem of overflow will occur in the process of doing arithmetic operations with vary large operands, which need to be taken into consideration.
4.2 Boolean Operator
1. Relational Operator
Relationship operator is used to compare the size of two operands, whose return value is boolean type.
name | symbol |
---|---|
more than | > |
less than | > |
no less than | >= |
no more than | <= |
equal | == |
different | != |
Characters will be compared according to their ASCII.
Double type can’t be transformed into floats without any error, so we should avoid comparing two floats directly using relational operator. Instead, we should compare their distance with a extremely small number, for example:
fabs(x - y) < 1e-6; //x == y fabs(x - y) > 1e-6; //x != y
2. Logic Operator
Logic operator is used to manipulate boolean types or expressions that evaluate to boolean types, whole return value is the value of its last evaluation.
name | symbol |
---|---|
and | && |
or | || |
not | ! |
Logic Operator follows the rule of **Short-circuit Evaluation **. That is to say when one operand is true, there’s no need to evaluate the remaining operands of
||
and when one operand is false, there’s no need to evaluate the remaining operands of&&
.The rule of short-circuit evaluation on one hand improves the efficiency of computing, on the other hand provide a guard for the remaining operands, which means the former operand can help avoid raising exceptions when evaluating the latter operands.
4.3 Bitwise Operator
Bit wise operator manipulates two binary numbers digit by digit.
1. Logical Bitwise Operator
name | symbol |
---|---|
bitwise not | ~ |
bitwise and | & |
bitwise or | | |
bitwise differ | ^ |
Operator
^
has a special feature:(x^a)^a = x
.
2. Shift Operation
name | symbol |
---|---|
left-shift | << |
right-shift | >> |
4.4 Assignment Operator
The value of a variable can be modified, either by input or by assignment statement.
1. Atomic Assignment Operator
=
needs two operands, and the former one must be an expression whose value is editable. The operator will assign the value of the expression to the right of it to the former operand.
In C++, unlike other programming language, assignment is provided as an operator rather than a statement, which has its own return value that is the left operand of
=
. So the change of the value of its left operand is=
's side effect.So,
(a = b) * c
means first give the value ofb
toa
, then multiplya
byb
.
2. Compound Assignment Operator
a #= b
is the same as a = a # b
, here #
is a binary operator.
4.5 Other Operators
1. Conditional Operator
name | symbol |
---|---|
conditional | <d1> ? <d2> : <d3> |
Conditional operator is a ternary operator, whose syntax is:
<d1> ? <d2> : <d3> //Here <d1> is usually a boolean expression whose return value is true or false. //if <d1> evaluates to true, then the return value of this expression will be thevalue of <d2>; //Otherwise, the return value of this expression will be the value of <d3>. (a > b) ? a : b //The return value of the expression above is the maximun of a and b.
2. Comma Operator
name | symbol |
---|---|
comma | <exp1>, <exp2> |
The meaning of
,
is to do each computation from left to right.x = a + b, y = c + d, z = x + y; //is the same as z = a + b + c + d;
3. Sizeof Operator
name | symbol |
---|---|
sizeof | sizeof<data-type> or sizeof<expression> |
sizeof computes the space it needs in the memeory by
byte
.