Number Repersentation
Number Bases
Number Bases是指对于数量描述的数字的基本规则,也就是进制。
Commonly Used Number Bases(进制)
- Decimal (base 10)
- Symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- Notation: 947 2 t e n 9472_{ten} 9472ten = 9472
- Binary (base 2)
- Symbols: 0, 1
- Notation: 10101 1 t w o 101011_{two} 101011two = 0b101011
- Hexadecimal (base 16)
- Symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- Notation: 2 A 5 D h e x 2A5D_{hex} 2A5Dhex = 0x2A5D
Q:Convert 0b11110 to decimal.
A:30 = 1*2^4 + 1*2^3 + 1*2^2 + 1*2^1 + 0*2^0
Big Idea:Bits can peprensent anything
- 可以用7bits(ASCII)表示大小写字母和标点符号
- 可以用0->False,1->True表示逻辑门
- 可以用01组合表示颜色 Red(00) Green(01) Blue(11)
- **ONLY ONE constraint: n digits (base B) ⇒ <=
B
n
B^n
Bn things
- Each of the n digits is one of B possible symbols
Unsigned integers
仅代表非负整数(无符号):
00
0
t
w
o
=
0
t
e
n
00
1
t
w
o
=
1
t
e
n
01
0
t
w
o
=
2
t
e
n
01
1
t
w
o
=
3
t
e
n
10
0
t
w
o
=
4
t
e
n
10
1
t
w
o
=
5
t
e
n
11
0
t
w
o
=
6
t
e
n
11
1
t
w
o
=
7
t
e
n
\begin{aligned} 000_{two} = 0_{ten} \\ 001_{two} = 1_{ten}\\ 010_{two} = 2_{ten}\\ 011_{two} = 3_{ten}\\ 100_{two} = 4_{ten}\\ 101_{two} = 5_{ten}\\ 110_{two} = 6_{ten}\\ 111_{two} = 7_{ten} \end{aligned}
000two=0ten001two=1ten010two=2ten011two=3ten100two=4ten101two=5ten110two=6ten111two=7ten
Most po number:
1
…
1
t
w
o
=
(
2
n
−
1
)
t
e
n
1 \dots 1_{two} = (2^n-1)_{ten}
1…1two=(2n−1)ten
Signed Integers
如何表示有符号数字?(±)
Let the first bit be interpreted as a sign
Sign and Magnitude
第一位作为符号标志,其他以无符进行运算
0表示+
1表示-
Bisaed Notation
0开的最后一个(变号前)的为0,0递减,1递增
Ones’s Complement
第一位表示正负,改变正负后仍然是递增。变号的两位绝对值相同
Two’s Complement(二进制补码)
第一位表示正负,改变正负后仍然是递增。变号的两位依然是绝对值递增(注意取值范围)
Two’s Complement Summary
- 适用于所有现代硬件
- 较为均匀地覆盖正负值
- 仍然可以使用最高有效位是符号位
- 取负:01翻转并+1
example:+7=0b 0000 0111, -7=0b 1111 1001
Q:我们有5bits,能表示哪个范围的二进制补码?
A:23+22+21+21=15,二进制补码为-16~15
Overflow(溢出)
数字可以有无穷 ∞ \infty ∞位数,但是计算机硬件只能存储有限数量的数。
- Usually ignore leading zeros
- Leftmost is most significant bit (MSB)
- Rightmost is least significant bit (LSB)
Overflow(溢出)是当算术运算的结果不能代表的硬件部分。通常是在数学上不正确的结果
例如:
无标志符:
0
b
1
…
1
+
1
10
=
0
b
0
…
0
=
0
?
0b1\dots 1+1_{10} = 0b0\dots 0 = 0?
0b1…1+110=0b0…0=0?
二进制补码:
0
b
1
…
1
+
1
10
=
0
b
0
…
0
=
−
2
(
n
−
1
)
t
e
n
?
0b1\dots 1+1_{10} = 0b0\dots 0 = {-2^{(n-1)}}_{ten}?
0b1…1+110=0b0…0=−2(n−1)ten?
显然因为硬件存储的问题,存在数字表示错误而产生了不同的结果
解决办法是增加更多的的位bits去表示
Sign Extension
希望使用比以前更多的位来表示相同的数字
- Easy for positive # s (add leading 0’s), more complicated for negative # s
- Sign and magnitude: add 0’s after the sign bit
- One’s complement: copy MSB
- Two’s complement: copy MSB
example: - Sign and magnitude:0b 11 = 0b 1001
- One’s/Two’s complement:0b 11 = 0b 1111
Summary
以上就是lec01的笔记,主要学习了
- 数字表示:如何使用二进制来表示正整数和负整数
- 无符号:基于2进制解释
- 有符号:二进制补码
- 运算:不同表示方式的二进制运算
- 符号拓展:必须保留有符号数字