BP-2-3 Operator

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
namesymbol
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.

namesymbol
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.

namesymbol
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
namesymbol
bitwise not~
bitwise and&
bitwise or|
bitwise differ^

Operator ^ has a special feature: (x^a)^a = x.

2. Shift Operation
namesymbol
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 of b to a, then multiply a by b.

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
namesymbol
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
namesymbol
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
namesymbol
sizeofsizeof<data-type> or sizeof<expression>

sizeof computes the space it needs in the memeory by byte.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值