补码的本质"/>
信息与编码,二进制数补码的本质
- what is “information”
Information resolves uncertainty. Information is simply that which cannot be predicted. The less predictable a message is, the more information it conveyed.
信息消除不确定性,一条消息越不可预测,传递的信息越多。
- 度量信息(Quantifying Information)
Suppose you’re faced with N equally probable choices, I give you a fact that narrows it down to M choices. Then I give you log2(N/M) bits information. (Claude Shannon, 1948)
概率相等地面对N中可能,一个事实将其降为M种可能,该事实包含了log2(N/M) 位的信息。
例子:
one coin flip: log2(2/1) = 1 bit
roll of 2 dice: log2(36/1) = 5.2 bits
- 编码(Encoding)
Encoding describes the process of assigning representations to information.
Choosing an approciate and efficient encoding is a real engineering challenge.
Impacts design at many levels: Mechaism (devices, # of components used), Efficient (bits), Reliable (noise), Security (encryption)
- 数的编码(Encoding numbers)
数制:表示数量的规则,由“位 + 进位规则”构成
码制:表示事物的规则
数制的编码,无论是二进制、十进制、十六进制等所有进制,都是各位上数值与权重(weight)的积的和。十进制“100”是编码,之所以表示“100”,是 1∗102 1 ∗ 10 2 。
- 二进制数的补码(2’s complement)
最高位为符号位 (0为正,1为负)
正数的补码与原码相同
负数的补码 = 原码逐位求反(反码) + 1
二进制数的补码的问题:
符号位是码制,并不代表数值,不能进行加法运算。补码的本质是给符号位赋予权重,使其表示数值。
8-bit 2’s complement example:
11010110 = −27+26+24+21 − 2 7 + 2 6 + 2 4 + 2 1 = -128 + 64 + 16 + 4 + 2 = -42
By moving the implicit location of “decimal” point, we can represent fractions too:
1101.0110 = −23+22+20+2−2+2−3 − 2 3 + 2 2 + 2 0 + 2 − 2 + 2 − 3 = -8 + 4 + 1 + 0.25 + 0.125 = -2.625
4和-4的补码表示
0100 = 0 0100 = 00 0100
100 = 1100 = 1 1100 = 11 1100
- 补码表示的二进制数相加的符号位
13 + 10、13 - 10、 -13 + 10 、 -13-10
// 考虑进位问题,需要6位0/1表示
// 13 + 10 = 23
0 01101 + 0 01010 = 0 10111// -13 + 10 = -3
1 10011 + 0 01010 = 1 11101// -13 - 10 = -23
1 10011 + 1 10110 = 1 01001
// 两个加数的符号位和来自最高位数字位的进位相加,结果就是和的符号。符号位溢出后舍弃。
参考:
:TsinghuaX+20250103X+sp+type@asset+block/First_Chapter_PPT.pdf
.html
更多推荐
信息与编码,二进制数补码的本质
发布评论