信息与编码,二进制数补码的本质

  • 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”,是 1102 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+22+23 − 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
// 两个加数的符号位和来自最高位数字位的进位相加,结果就是和的符号。符号位溢出后舍弃。

参考:
http://www.xuetangx.com/asset-v1:TsinghuaX+20250103X+sp+type@asset+block/First_Chapter_PPT.pdf
http://www.ruanyifeng.com/blog/2009/08/twos_complement.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值