数的表示2

一、数的表示

1、POSITIONAL NOTATION

就是“权重”的概念

2、Additive Notation

古罗马计数法,有所有数字加起来表示一个数

二、数制转化

1、基于的公式

(1)Base小的转化为Base大的:在目的Base下展开,在目的Base下做加法

x = dk2k+ dk-12k- 1+....+d121+d020

可以写作x = d0 + 2(d1+2(d2+.....+2(dk-1+2dk)...))

(2)Base大的转化为Base小的:整数部分在目的Base下除以原Base取余,小数部分乘以原Base取整。

Because -the "mod" function gives us back the remainder of the division of thenumber by 2, and this is the low order digit. That is:

(dk2k+ dk-12k- 1+....+d121+d020)mod 2 = (2 (dk2k-1 + dk-12k-2+....+d120) + d020)mod 2 = d020

三、硬件使用二进制的原因

Electronic devices that are based on only two easily distinguishable states are cheaper and more reliablethan devices based on more than two states.

四、半加器与全加器

1、半加器:将两个东西(数1、数2)相加的电路

2、全加器:将三个东西(数1、数2、进位)相加的电路

五、2的补码

1、为什么叫做2的补码?

(1)先考虑10进制的情况

考虑A - B =A -10 + (10 - B) ,其中 (10 - B) 被称作B的10的补码( "10'scomplement" of B

然后,考虑(10 -B)= ((9 - B) + 1),其中(9 - B) 是B的9的补码( "9's complement" of B)。而9的补码比10的补码要好,原因是9的补码将求补运算的被减数(9而不是10)何其结果限制在了1位(0的10’C结果为10,为两位数)。

Digit

9's Complement

0

9

1

8

2

7

3

6

4

5

5

4

6

3

7

2

8

1

9

0

For any decimal value of X and Ynot greater than 99999 we can show that:

X + Yis equal to X - 100000 + (100000 - abs(Y))
That is, X - 100000 + (99999 - abs(Y) + 1)

Where 99999 - abs(Y) = 9'scomplement of Y and (99999 - abs(Y) + 1) is the 10's complementof Y

这里的99999 - abs(Y)并不称作:“99999的补码”,而是称作“9的补码”。

也即:“X的补码”的全称应该是“X的按位补码”(其实是对每一位求补)。

(2)再考虑2进制的情况

For binary representation in the computerthe problem is easier since (a) the number of digits is only 2, and (b) and wealways know how many digits are in each representation. The two complementaryrepresentations are called "2's complement" and "1'scomplement".

Digit

1's Complement

0

1

1

0

and the 2's complement is simply the 1'scomplement plus 1!

2、为何2的补码=反码+1?

1的补码又称为反码,通过上述(2)即可看出

3、使用2的补码(以下简称2C)求解减法的实质

(1)

To perform the operation (A-B), wefirstly modify our computation to (A-K)+(K-B)
where K = 2n such that n is the number of digits(bits) in B, that is n = floor(log2|B|) + 1,
and where (K-B) is the 2's complement of B.

Algorithm:

  1. Given A and B,
  2. Complement B to produce (K-B),
  3. Add A and complement B,
  4. Subtract K from the result.

 

所谓:正数的2C是本身,负数的2C是其反码+1这种说法,不如上述说法更加反映本质。之所以“正数的2C是本身,负数的2C是其反码+1”是因为

(2)例子1

所以最后要减去K,也即将结果限制在减数和被减数的位数上。例如:

若要计算2710-1110

011011
+ 110101
_______
1010000

此时要去掉开头的1,也即最终结果为:010000

(3)例子2

若要计算1110-2710

010101
+ 100101
_______
0111010

此时的0不能够去掉,因为:

(4)更为普遍的例子,更能揭示本质:

EXAMPLE - NORMAL CASE A > B:

01101011

-

1110

 = 

01101011 - 100000000

+

100000000 - 00001110

 = 

01101011 - 100000000

+

11111111 - 00001110 + 1

 = 

01101011 - 100000000

+

complement (00001110) + 1

 = 

01101011 - 100000000

+

11110001 + 1

 = 

01101011 - 100000000

+

11110010

 = 

101011101 - 100000000

 = 

 

 

01011101

 

 

 

EXAMPLE - CASE A < B:

1110

-

1101011

 = 

00001110 - 100000000

+

100000000 - 01101011

 = 

00001110 - 100000000

+

11111111 - 01101011 + 1

 = 

00001110 - 100000000

+

complement (01101011) + 1

 = 

00001110 - 100000000

+

10010100 + 1

 = 

00001110 - 100000000

+

10010101

 = 

10100011 - 100000000

 = 

 

 

-01011101

 

That is 10100011!

 

这是在普通域的运算,不是在补码域的运算,但是这样就揭示了补码的合理性(其运算符合普通域的规定)。

  • Given n bits, there are
        2n-1 - 1 possible positive numbers,
        2n-1 possible negative numbers, and 1 zero
  • Two's complement allows arithmetic to be implemented very efficiently in hardware. 补码高效的原因:
    • Addition does not have to be concerned with the sign(s) of the numbers, and subtraction can be implemented by complementing the subtrahend and then adding. 补码在做加减法的时候不需考虑符号位,因为符号位在补码域内恰好被正确的使用了(具体原因在第三条)。
    • Multiplication can be implemented without regard to the sign of either number with only minor amendments, and similarly for division.
    • The most significant bit can still be viewed as a sign bit since a given bit string represents a negative number if and only if the leading bit is one. 结果的最高位仍可视为符号位,这样符号位的处理变成了符号位的运算,非常方便。

六、一个想法

在讨论数的表示法的时候,必须抛开数的表示法来考虑问题。

就是说考虑的时候只关心数的Value,而不是其表现形式。不同的表现形式与其Value之间有一一对应的数值关系,可以由表达式表达,进而加以分析。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值