計算機実習 I

第二回 (2016 年 4 月 14日)

演算子の種類、使い方、優先度

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

Martin J. Dürst

AGU

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

今日の予定

 

ミニテスト

 

前回の課題について

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

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

 

前回の演習について

 

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/8/assignments

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

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

 

Timeline

Caution: Programming efficiency decreases rapidly in the late evening. 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

 

インデント評価の表示

 

インデント評価の点数

 

 インデントの評価の注意点

 

プログラム変換

 

Operators

 

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)

a -= b += ca -= (b+=c) (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),...

A comic about binary notation

 

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 参照)

 

今週の演習

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

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

 

来週のための準備

 

Glossary

format
形式
indent
インデント、字下げ
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
右シフト