Operators: Classification, Usages, Priority
(演算子:
演算子の種類、使い方、優先度;
演算子とデータ型の関係)
Computing Practice I
2nd lecture, April 20, 2017
http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture2.html
Martin J. Dürst
© 2005-17 Martin
J. Dürst 青山学院大学
Today's Schedule
- Minitest
- Last week's exercises
- Environment and timeline
- Program formatting (intents)
- Operators
- Today's exercises
ミニテスト
- 授業開始までにパソコンにログイン
- ブラウザで http://moo.sw.it.aoyama.ac.jp
へ移動、ログイン
- Computer Programming I に入る (未登録の学生は enrollment key
を使って授業登録)
- ナビゲーションは左に畳み、ブラウザは全画面に拡大
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄を椅子の下に
- テスト終了後その場で待つ
About Last Week's Exercises
- Always compile and test your program on your PC before submitting
- However, results may differ between PC and server (example:
initialization)
- Number of submissions
|
01A1 |
01A2 |
01A3 |
01B1 |
01B2 |
01C1 |
100 points |
87 |
81 |
72 |
60 |
57 |
54 |
60 points |
12 |
18 |
27 |
39 |
42 |
44 |
error |
- |
- |
- |
- |
- |
1 |
How to Best Use the Q&A Forum
- Due to system problems, not many questions last week
- The Q&A Forum saves time for similar questions
Example: 「warning: no newline at end of file」
- Never remove questions
- The Q&A Forum has a very useful search function (upper right)
- The Q&A Forum is also part of the course evaluation and
tests/examination
- The Q&A Forum can also be used after the lecture
(Friday/Saturday)
- On Friday, you can come to my Lab (O-527/O-529) in the late afternoon
(16:35~)
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
- Thursday 15:05: Start (everybody logged in to Moodle)
- Thursday 17:00, 17:30,...: Points decreased to 60% (Preferred
deadline)
- Thursday during exercises: Make sure you take breaks to eat and drink
- Thursday before 18:20: If you finished all problems, get my permission
before leaving
- Thursday 20:00: N604b shuts down
- Saturday 24:00: Deadline for Q&A questions (for assignments with a
Monday deadline, Sunday 24:00)
- Sunday 22:00: Deadline for assignment submission
- Monday 22:00: Deadline for some assignment submissions
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
- Correctly indented programs are easy to read
- Programs without indents are difficult to read and confusing
- Before you ask a teacher (TA) a question or submit a question to the
Q&A forum, make sure your program looks good
- Keep indents correct all the time
- This helps getting better at programming
- Indents are part of grades starting from this lecture
Indent Evaluation: Display
- Only displayed immediately after submission
- Number of errors (e.g.: Number of Indentation Errors: 5)
- Places with not enough indenting are marked yellow
- Places with too much identing are marked pink
- Other problems (newlines, spacing) are marked orange
- Details are show when placing the cursor above the colored area
- Check adapts to various indentation styles
- Uniformity is very important
- There are some minimal requirements
(e.g. space after #include
, newline before {
at
start of function body)
Indent Evaluation: Points
- Points are shown on the Best
Attempts and All
Attempts pages
- Points are calculated on the submission with the worst indenting
- Correct Indents per Line shows percentage of correctly indented lines
- Indent Adjustment shows point added or deducted
- Calculation:
- No mistakes found:
+5
- More than 90% correct: --
- 0% or less correct:
-18
- Proportional to total number of points (
60points
、partial points)
Indents: Caution
- Evaluation is not perfect
- Improvement may be possible (please provide ideas)
- Display may also still need improvements (please provide ideas)
- Please carefully check example programs (e.g. in the book used for the
lecture)
- Work on better indenting not for the points, but to get better at
programming
Operators
- Operator categories
- Types of operands
- Arity (unary, binary, ternary)
- Homographs
- Priority (p. 388)
- Associativity (left-associative, right-associative)
- Evaluation order and shortcuts
Operator Categories
- Arithmetic operators (arithmetic operators,
+
,
-
, *
, /
, %
)
- Relational operators (relational operators,
==
,
!=
, <
, >
, <=
,
>=
)
- Logical operators (logical operators,
||
,
&&
, !
)
- Increment/decrement (
++
, --
)
- Bitwise operators,
|
, &
, ^
,
~
, <<
,
>>
)
- Assignment operators,
=
, +=
, -=
,
*=
, /=
, %=
, ...)
- Type cast (
()
)
- Conditional operator (
? :
) and sequence operator
(,
)
- Operators for arrays, pointers, and structures (
[]
,
*
, .
, ->
)
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 += f
⇔ d -= (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
- Applications of bitwise operators and shift operators:
High-speed low-level processing, representation of sets/settings
|
: Bitwise or (logical or: ||
)
&
: Bitwise and (logical and:
&&
)
^
: Bitwise exclusive or (there is no logical exclusive
or)
~
: Bitwise not (logical not: !
)
<<
: left shift
>>
: right shift
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 ·
27 ⇔ 5 << 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 参照)
()
: 関数、型変換
(cast)、演算のまとめ、if
文など
,
: 引数の区切り、順序演算
-
: 単項: 符号反転; 二項: 引き算
+
: 単項 (型変換以外) 効果なし; 二項:
足し算
*
: 単項: 参照演算子 (ポインタ); 二項:
掛け算
&
: 単項: アドレス演算子 (ポインタ); 二項:
ビット毎積
Today's Exercises
自分の PC に Cygwin (gcc,...)
がインストールできなかった場合、早めに相談
先週授業に登録しなかった学生は素早く連絡のこと!
- 02A1, 02A2, 02A3: 予想を紙に記入;
間違ってもいいが、よく考える
早めに先生に提出 (最終期限は 18:30)
- 02B1: 素数のちょっと変わった計算方法
- 02C1: ビット毎演算で足し算
- プログラムの開発中は途中結果も出力
- 最終的なプログラムは結果だけ出力
- 02C2: 9x9
を覚えてない人のための掛け算、発展問題、締切が月曜日
来週のための準備
- 演習問題が残ったら宿題として完成、Program Checking
System に投稿
- 今日の復習
- 教科書の第 4 章、第 5 章を読むこと (pp. 75-122)
注意: 読んだ章もミニテストなどの質問対象
Glossary
- 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
- 右シフト