本课大纲:
l1.lower-lever C constructs
2.High-lever C++ constructs
3.construct programming
4.scheme
5.python
本节主要内容:数据如何在内存中存储,以0和1的方式,以及如何表示出来。
基础知识:
C/C++ | |
bool | |
char | 1 byte |
short | 2 bytes |
int | 4 bytes |
long | 4 bytes |
float | 8 bytes |
double |
a single bit can sore a zero or a one,1 byte == 8 bytes
0/1 | 0/1 | 0/1 | 0/1 | 0/1 | 0/1 | 0/1 | 0/1 |
1 byte with 8 bits can choose zero or one, so == 256.
for example,65 = 64+1 = +
,so
0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
举例:
short:2 bytes == 16 bits
0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
= 512.
蓝色位处表示符号,0正1负。
2补码:负数取反。
加入补码的原因是,如果使用传统二进制计算,那么相反数相加并不一定等于0.补码之后,就像多米诺骨牌一样,成为0.
存入输出:
char ch = 'A';
short s = ch;
cout << s << end;
0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
其他同理可得,但若是上超出下,超出部分舍弃。
符号拓展部分落下不记,会了再说。
float比较特殊。
+/- | 8位——exp | 32位————————小数位.xxxxxx |
exp:0 - 225
小数位:1-(无限接近于0.9)
总:;
例子: 5.0 = 1.25*
exp = 129,小数位0.25.