http://www.sw.it.aoyama.ac.jp/2016/CP1/lecture2.html
© 2005-16 Martin J. Dürst 青山学院大学
01A3: 2147483647+1 ⇒ -2147483648 はなぜ
十進法 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 |
二進法 (2の補数) | 100 | 101 | 110 | 111 | 000 | 001 | 010 | 011 |
01C1:
[エラー08] の解答: \n
を \t
(タブ)
に変更
01A1 | 01A2 | 01A3 | 01B1 | 01B2 | 01C1 | |
100点 | 83 | 81 | 68 | 40 | 40 | 31 |
60点 | 6 | 8 | 21 | 48 | 47 | 46 |
エラー | - | - | - | - | 1 | 1 |
未提出 | - | - | - | 1 | 1 | 11 |
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
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
Which of the following two programs is easier to read?
int main (void){ |
int main (void) |
+5
-18
60点
、部分点) に比例gcc 02B1.c && ./a
を推奨)+
,
-
, *
, /
, %
)==
,
!=
, <
, >
, <=
,
>=
)||
,
&&
, !
)++
, --
)|
, &
, ^
,
~
, <<
,
>>
)=
, +=
, -=
,
*=
, /=
, %=
, ...)()
)? :
) and sequence 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)
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 += c
⇔ a -= (b+=c)
(right-associative)
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 or (logical or: ||
)&
: Bitwise and (logical and:
&&
)^
: Bitwise exclusive or (there is no logical exclusive
or)~
: Bitwise not (logical not: !
)<<
: left shift>>
: right shiftCaution: Because numbers are always represented in binary inside the computer, there is no need for special operations for input/output.
(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
),...
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
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,...
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 |
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
)
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
文など,
: 引数の区切り、順序演算-
: 単項: 符号反転; 二項: 引き算+
: 単項 (型変換以外) 効果なし; 二項:
足し算*
: 単項: 参照演算子 (ポインタ); 二項:
掛け算&
: 単項: アドレス演算子 (ポインタ); 二項:
ビット毎積自分の PC に Cygwin (gcc,...) がインストールできなかった場合、早めに相談
先週授業に登録しなかった学生は素早く連絡のこと!