Operators: Classification, Usages, Priority

(演算子: 演算子の種類、使い方、優先度; 演算子とデータ型の関係)

Computing Practice I

2nd lecture, April 19, 2018

http://www.sw.it.aoyama.ac.jp/2018/CP1/lecture2.html

Martin J. Dürst

AGU

© 2005-18 Martin J. Dürst 青山学院大学

Today's Schedule

 

ミニテスト

 

前回の課題について

01A2: 三人も松尾芭蕉の「蛙...」の俳句を使用

01A3: 2147483647+1 ⇒ -2147483648 はなぜ

01C1: [エラー08] の解答: \n\t (タブ) に変更

 

About Last Week's Exercises

 

Initialization

Which of the following two program fragments is better?

int i, k;
for (i=0; i<10; i++) { k += i;
printf("%d\n", k);
}
int i, k=0;
for (i=0; i<10; i++) { k += i;
printf("%d\n", k);
}
result undefined result defined

 

How to Best Use the Q&A Forum

 

Convenient Links in the
Program Checking System

List of assignments:
http://rpcsr.sw.it.aoyama.ac.jp/courses/10/assignments

Best Attemts (per assignment), including not yet submitted assignments:
http://rpcsr.sw.it.aoyama.ac.jp/attempts?select=best

All Attempts (excluding not yet submitted assignments):
http://rpcsr.sw.it.aoyama.ac.jp/attempts

 

Timeline

Caution: Programming efficiency decreases rapidly late at night. Better to go to bed and give it another try the next morning

Caution: Because it is not possible to ask questions on the day of the deadline, do your best on the day before the deadline to be able to ask questions

 

Program Formatting

Which of the following two programs is easier to read?

int main (void){
int i;double p;
for(i=1;i<=10;i++){ p=pow(2.64,i);
printf("%lf\n",p);
}return 0;
}
int main (void)
{
int i; double p;

for (i=1; i<=10; i++) { p = pow(2.64, i);
printf("%lf\n", p);
}

return 0;
}

 

Spaces and Indents

 

Indent Evaluation: Display

 

Indent Evaluation: Points

 

 Indents: Caution

 

No Newline at End of File

 

Operators

 

Priority and Associativity

(see p. 388)

How is a = b . c += d - e + f ^ g & h evaluated?

a = (1b.c)1 += [3(2d-e)2+f]3 ^ g & h

a = (1b.c)1 += [5[3(2d-e)2+f]3^(4g&h)4]5

{7a={6(1b.c)1+=[5[3(2d-e)2+f]3^(4g&h)4]5}6}7

Why?

 

Operator Categories

 

Modulo Operator

(see Discrete Mathematics I 合同算術参照)

% calculates the remainder of an integer division

Example: 22 % 5 ⇒ 2

Applications: Encryption, advanced combination of loops, table formatting (see プログラマの数学, Chapter 3)

 

Assignment Operators

Combination of calculation and assignment

Example: a = a + b;a += b;

Also: -=, *=, /=, %=, <<=, >>=, &=, |=, ^=

a += b is shorter than a = a + b and easier to understand as a unit

Assignment operators are right-associative

Example:

a - b + c(a-b) + c (left-associative)

d -= e += fd -= (e+=f) (right-associative)

 

Increment/Decrement

a = a + 1;a += 1;++a;a++; (increment)

Prefix increment: b = ++a;b = (a+=1);

Postfix increment: b = a++;b = a; a += 1;

Similar for decrement (--)

 

参考: 副作用

(side effect)

 

Bitwise Operators

Caution: Because numbers are always represented in binary inside the computer, there is no need for special operations for input/output.

 

Example of Bitwise OR

(Examples are for 16 bits (short). int is 32 bits or 64 bits on most PCs)


variables/formula decimal bits
a 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
b 56 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
a | b 58 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0

Applications: Setting bits (to set the four lowest bits in a: a |= 0xF),...

 

Example of Bitwise AND


variables/formula decimal bits
a 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
b 56 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
a & b 8 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0

Applications: Extracting or clearing bits
To extract the four lowest bits in a: a & 0xF

To clear the four lowest bits in a: a &= ~0xF

 

Example of Bitwise XOR (Exclusive OR)


variables/formula decimal bits
a 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
b 56 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
a ^ b 50 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 0

Applications: Encription,...

 

Example of Bitwise NOT


variables/formula decimal bits
a 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
~a -32758 (signed) or 65525 (unsigned) 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1

 

Example of Left Shift


variables/formula decimal bits
a 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
a << 5 320 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0

Applications: Multiplication by 2n,... (5 · 275 << 7)

 

Example of Right Shift


variables/formula decimal bits
a 56 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0
a >> 4 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1

Applications: Division by 2n,...

 

参考: 同形多義の演算子や記号

(教科書 p.388/389 参照)

 

Today's Exercises

自分の PC に Cygwin (gcc,...) がインストールできなかった場合、早めに相談

先週授業に登録しなかった学生は素早く連絡のこと!

 

来週のための準備

 

Glossary

initialization
初期化
program fragment
プログラム断片
define a variable
変数を定義する
format
形式
indent
インデント、字下げ
uniformity
統一性
operator
演算子
operand
被演算子
arity
項の数、引数の数
unary (operator/function)
単項
binary (operator/function)
二項
ternary (operator/function)
三項
homograph
同型多義
priority
優先度
associativity
結合 (方向)
left-associative
左結合
right-associative
右結合
arithmetic operator
算術演算子
relational operator
関係演算子
logical operators
論理演算子
increment
インクリメント
decrement
デクリメント
prefix increment
前置インクレメント
postfix increment
後置インクレメント
bitwise operators
ビット毎演算子
assignment operator
代入演算子
type cast
型変換演算子
conditional operator
条件演算子
sequence operator
順次演算子
array
配列
pointer
ポインタ
structure
構造体
modulo operator
剰余演算子
encryption
暗号化
signed
符号付き
unsigned
符号なし
left shift
左シフト
right shift
右シフト