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
© 2005-18 Martin
J. Dürst 青山学院大学
Today's Schedule
- Minitest
- Last week's exercises
- Environment and timeline
- Program formatting (indents)
- Operators
- Today's exercises
ミニテスト
- 授業開始までにパソコンにログイン
- ブラウザで http://moo.sw.it.aoyama.ac.jp
へ移動、ログイン
- Computer Programming I に入る
- ナビゲーションは左に畳み、ブラウザは全画面に拡大
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄を椅子の下に
- テスト終了後その場で待つ
前回の課題について
01A2: 三人も松尾芭蕉の「蛙...」の俳句を使用
01A3: 2147483647+1 ⇒ -2147483648 はなぜ
- 負の整数の表現は「2の補数」(two's complement)
- n ビットで表現できるのは
-2n-1 ... 2n-1-1
- 実例 (n = 3):
十進法 |
-4 |
-3 |
-2 |
-1 |
0 |
1 |
2 |
3 |
二進法 (2の補数) |
100 |
101 |
110 |
111 |
000 |
001 |
010 |
011 |
- 実際は n = 32 (又は n = 64) が多い
- 2の補数の場合、負の数の足し算に特別な配慮は不要
(02C1 参照)
- オーバフローが起こりうる
01C1: [エラー08] の解答: \n
を \t
(タブ)
に変更
About Last Week's Exercises
- Always compile and test your program on your PC before submitting
- However, results may differ between PC and server
- Number of submissions
|
01A1 |
01A2 |
01A3 |
01B1 |
01B2 |
01C1 |
100 points |
76 |
73 |
67 |
42 |
42 |
47 |
60 points |
21 |
24 |
26 |
52 |
47 |
40 |
error |
1 |
1 |
3 |
1 |
3 |
2 |
not submitted |
1 |
1 |
1 |
4 |
7 |
10 |
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 |
- Always set variables before using them
- The best way to set variables from the start is initialization
- Initialization means the variable is set when defined
- Variables that are not set are undefined
- Most differences between Cygwin and the Program Checking System are due
to undefined values (missing initializations)
How to Best Use the Q&A Forum
- The Q&A Forum saves time for similar questions
Example: 「warning: no newline at end of file」
- Never remove questions
- No need for confirmation/thank you reply
- 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 afternoon
(14:00~)
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
- 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 after 18:20: No need to ask for permission to leave if all
problems finished
- 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 advanced assignment
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 points 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
No Newline at End of File
- Make sure you do not indent the last closing
}
- Move with ← and → keys over last lines of file to find unnecessary
spaces
- Use View ... Show Whitespace in your editor to show unnecessary
spaces
Operators
- Priority
- Associativity
- Operator categories
- Types of operands
- Arity (unary, binary, ternary)
- Homographs
- Evaluation order and shortcuts
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?
- Priority (highest to lowest) is:
.
,
+
/-
, &
, ^
,
+=
/=
+/-
are
left-associative (→)
+=/=
are right-associative (←)
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
),...
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
- 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
- 右シフト